Cover image for LeetCode Hot 100 P543 Diameter of Binary Tree

LeetCode Hot 100 P543 Diameter of Binary Tree

Words 340
Views
Visitors

Timeline

Timeline

2026-03-14

init

Post-order traversal, depth of binary tree

Problem:

Iterative approach:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
struct TreeNode {        int val;        TreeNode *left;        TreeNode *right;        TreeNode()                : val(0)                , left(nullptr)                , right(nullptr)        {        }        TreeNode(int x)                : val(x)                , left(nullptr)                , right(nullptr)        {        }        TreeNode(int x, TreeNode *left, TreeNode *right)                : val(x)                , left(left)                , right(right)        {        }};#include <stack>#include <unordered_map>using std::stack;using std::unordered_map;class Solution {    public:        int diameterOfBinaryTree(TreeNode *root)        {                // For each node, the maximum of left subtree depth + right subtree depth                stack<TreeNode *> stk;                unordered_map<TreeNode *, int> node_depth;                TreeNode *last = nullptr, *p = root;                int res = 0, left_depth, right_depth;                while (p || !stk.empty()) {                        if (p) {                                stk.push(p);                                p = p->left;                        } else {                                p = stk.top();                                if (p->right && last != p->right) {                                        p = p->right;                                } else {                                        // visit p                                        left_depth = (p->left) ? node_depth[p->left] : 0;                                        right_depth = (p->right) ? node_depth[p->right] : 0;                                        node_depth[p] = std::max(left_depth, right_depth) + 1;                                        res = std::max(res, left_depth + right_depth);                                        stk.pop();                                        last = p;                                        p = nullptr;                                }                        }                }                return res;        }};

Recursive approach: actually it is to find the maximum of the sum of left and right subtree depths for each node

123456789101112131415161718192021
class Solution {        int ans;        int depth(TreeNode *rt)        {                if (rt == NULL) {                        return 0; // Reached a null node, return 0                }                int L = depth(rt->left); // Depth of the subtree rooted at the left child                int R = depth(rt->right); // Depth of the subtree rooted at the right child                ans = max(ans, L + R + 1); // Compute d_node = L + R + 1 and update ans                return max(L, R) + 1; // Return the depth of the subtree rooted at this node        }    public:        int diameterOfBinaryTree(TreeNode *root)        {                ans = 1;                depth(root);                return ans - 1;        }};
Loading comments…