Why is my method going into infinite recursion?

前端 未结 6 1357
心在旅途
心在旅途 2021-01-15 11:34

I\'ve written a method to help build a quadtree. Each quadtree has a root node, and a root node has 4 children. I\'m using depth recursion to stop this function from dividin

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 11:57

    Your handling on the recursive call to buildTreeHelper seems perfectly correct. You check the depth terminating condition before recursing, and you always call it with depth-1.

    The only potential prolem I can see is if it's the constructor calls (the ones creating the children) which call buildTreeHelper again. That could result in infinite recursion if, for example, the constructor always calls buildTreeHelper with a object variable for depth (it would have the same or a greater value each time).

提交回复
热议问题