Finding max depth of binary tree without recursion

前端 未结 7 952
夕颜
夕颜 2020-12-24 09:23

Recursive mechanism to find max depth of depth of binary tree is very straightforward, but how can we do it efficiently without recursion as I have large tree where I would

7条回答
  •  孤独总比滥情好
    2020-12-24 09:42

    The recursive approach you've described is essentially a DFS over the binary tree. You can implement this iteratively if you'd like by storing an explicit stack of nodes and keeping track of the maximum depth encountered.

    Hope this helps!

提交回复
热议问题