You are viewing a single comment's thread. Return to all comments →
Python 3 Using an explicit stack instead of recursion
def height(root): stack = [(root, 0)] max_height = 0 while stack: node, curr_height = stack.pop() if (node.left is None) and (node.right is None): max_height = max((max_height, curr_height)) continue if not (node.left is None): stack.append((node.left, curr_height+1)) if not (node.right is None): stack.append((node.right, curr_height+1)) return max_height
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Height of a Binary Tree
You are viewing a single comment's thread. Return to all comments →
Python 3 Using an explicit stack instead of recursion