You are viewing a single comment's thread. Return to all comments →
C++ using a stack and iteration instead of recursion.
int height(Node* root) { struct Frame { Node* node{}; int height = 0; }; std::stack<Frame> stack; stack.push({root}); int max = INT_MIN; while (!stack.empty()) { const auto top = stack.top(); stack.pop(); max = std::max(max, top.height); if (top.node->right) stack.push({top.node->right, top.height + 1}); if (top.node->left) stack.push({top.node->left, top.height + 1}); } return max; }
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 →
C++ using a stack and iteration instead of recursion.