Episode 7 — DSA with JavaScript / 7.16 — Trees

7.16.a — Binary Tree Fundamentals

<< 7.16 Overview


What is a binary tree?

A binary tree is a hierarchical data structure where each node has at most two children (left and right).

        1           ← root
       / \
      2   3         ← children of 1
     / \   \
    4   5   6       ← leaves: 4, 5, 6

Terminology:
  Root:   1 (top node)
  Leaf:   Node with no children (4, 5, 6)
  Height: Longest path from root to leaf (2)
  Depth:  Distance from root to a node
  Level:  Set of nodes at same depth

Node definition

class TreeNode {
    constructor(val, left = null, right = null) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
struct TreeNode {
    int val;
    TreeNode *left, *right;
    TreeNode(int v) : val(v), left(nullptr), right(nullptr) {}
};

Types of binary trees

Full:           Complete:        Perfect:
    1               1               1
   / \             / \             / \
  2   3           2   3           2   3
 / \             / \             / \ / \
4   5           4   5           4  5 6  7

Full: every node has 0 or 2 children
Complete: all levels full except last, filled left to right
Perfect: all levels completely full

Traversals

Inorder (Left → Root → Right)

function inorder(node, result = []) {
    if (!node) return result;
    inorder(node.left, result);
    result.push(node.val);
    inorder(node.right, result);
    return result;
}

Preorder (Root → Left → Right)

function preorder(node, result = []) {
    if (!node) return result;
    result.push(node.val);
    preorder(node.left, result);
    preorder(node.right, result);
    return result;
}

Postorder (Left → Right → Root)

function postorder(node, result = []) {
    if (!node) return result;
    postorder(node.left, result);
    postorder(node.right, result);
    result.push(node.val);
    return result;
}

Level-order (BFS)

function levelOrder(root) {
    if (!root) return [];
    const result = [], queue = [root];
    while (queue.length) {
        const level = [], size = queue.length;
        for (let i = 0; i < size; i++) {
            const node = queue.shift();
            level.push(node.val);
            if (node.left) queue.push(node.left);
            if (node.right) queue.push(node.right);
        }
        result.push(level);
    }
    return result;
}
Tree:       1
           / \
          2   3
         / \
        4   5

Inorder:    [4, 2, 5, 1, 3]
Preorder:   [1, 2, 4, 5, 3]
Postorder:  [4, 5, 2, 3, 1]
Level-order: [[1], [2, 3], [4, 5]]

Height and diameter

function height(root) {
    if (!root) return -1;
    return 1 + Math.max(height(root.left), height(root.right));
}

function diameter(root) {
    let maxDia = 0;
    function h(node) {
        if (!node) return -1;
        const l = h(node.left), r = h(node.right);
        maxDia = Math.max(maxDia, l + r + 2);
        return 1 + Math.max(l, r);
    }
    h(root);
    return maxDia;
}

Lowest Common Ancestor (LCA)

function lca(root, p, q) {
    if (!root || root === p || root === q) return root;
    const left = lca(root.left, p, q);
    const right = lca(root.right, p, q);
    if (left && right) return root;
    return left || right;
}

Check symmetry

function isSymmetric(root) {
    function mirror(a, b) {
        if (!a && !b) return true;
        if (!a || !b) return false;
        return a.val === b.val && mirror(a.left, b.right) && mirror(a.right, b.left);
    }
    return !root || mirror(root.left, root.right);
}

Scenario bank (trees)

7.16a-001 — Invert binary tree (v1)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-002 — Check if balanced (v2)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-003 — Maximum depth (v3)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-004 — Minimum depth (v4)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-005 — Path sum (v5)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-006 — All root-to-leaf paths (v6)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-007 — Serialize and deserialize (v7)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-008 — Count complete tree nodes (v8)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-009 — Right side view (v9)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-010 — Zigzag level order (v10)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-011 — Flatten to linked list (v11)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-012 — Construct from preorder and inorder (v12)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-013 — Construct from postorder and inorder (v13)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-014 — Sum of left leaves (v14)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-015 — Binary tree maximum path sum (v15)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-016 — Vertical order traversal (v16)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-017 — Boundary traversal (v17)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-018 — Check subtree (v18)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-019 — Kth smallest in BST (v19)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-020 — Morris traversal (O(1) space) (v20)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-021 — Invert binary tree (v21)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-022 — Check if balanced (v22)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-023 — Maximum depth (v23)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-024 — Minimum depth (v24)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-025 — Path sum (v25)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-026 — All root-to-leaf paths (v26)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-027 — Serialize and deserialize (v27)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-028 — Count complete tree nodes (v28)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-029 — Right side view (v29)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-030 — Zigzag level order (v30)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-031 — Flatten to linked list (v31)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-032 — Construct from preorder and inorder (v32)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-033 — Construct from postorder and inorder (v33)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-034 — Sum of left leaves (v34)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-035 — Binary tree maximum path sum (v35)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-036 — Vertical order traversal (v36)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-037 — Boundary traversal (v37)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-038 — Check subtree (v38)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-039 — Kth smallest in BST (v39)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-040 — Morris traversal (O(1) space) (v40)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-041 — Invert binary tree (v41)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-042 — Check if balanced (v42)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-043 — Maximum depth (v43)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-044 — Minimum depth (v44)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-045 — Path sum (v45)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-046 — All root-to-leaf paths (v46)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-047 — Serialize and deserialize (v47)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-048 — Count complete tree nodes (v48)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-049 — Right side view (v49)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-050 — Zigzag level order (v50)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-051 — Flatten to linked list (v51)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-052 — Construct from preorder and inorder (v52)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-053 — Construct from postorder and inorder (v53)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-054 — Sum of left leaves (v54)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-055 — Binary tree maximum path sum (v55)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-056 — Vertical order traversal (v56)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-057 — Boundary traversal (v57)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-058 — Check subtree (v58)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-059 — Kth smallest in BST (v59)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-060 — Morris traversal (O(1) space) (v60)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-061 — Invert binary tree (v61)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-062 — Check if balanced (v62)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-063 — Maximum depth (v63)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-064 — Minimum depth (v64)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-065 — Path sum (v65)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-066 — All root-to-leaf paths (v66)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-067 — Serialize and deserialize (v67)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-068 — Count complete tree nodes (v68)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-069 — Right side view (v69)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-070 — Zigzag level order (v70)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-071 — Flatten to linked list (v71)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-072 — Construct from preorder and inorder (v72)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-073 — Construct from postorder and inorder (v73)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-074 — Sum of left leaves (v74)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-075 — Binary tree maximum path sum (v75)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-076 — Vertical order traversal (v76)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-077 — Boundary traversal (v77)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-078 — Check subtree (v78)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-079 — Kth smallest in BST (v79)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-080 — Morris traversal (O(1) space) (v80)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-081 — Invert binary tree (v81)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-082 — Check if balanced (v82)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-083 — Maximum depth (v83)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-084 — Minimum depth (v84)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-085 — Path sum (v85)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-086 — All root-to-leaf paths (v86)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-087 — Serialize and deserialize (v87)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-088 — Count complete tree nodes (v88)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-089 — Right side view (v89)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-090 — Zigzag level order (v90)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-091 — Flatten to linked list (v91)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-092 — Construct from preorder and inorder (v92)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-093 — Construct from postorder and inorder (v93)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-094 — Sum of left leaves (v94)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-095 — Binary tree maximum path sum (v95)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-096 — Vertical order traversal (v96)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-097 — Boundary traversal (v97)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-098 — Check subtree (v98)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-099 — Kth smallest in BST (v99)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-100 — Morris traversal (O(1) space) (v100)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-101 — Invert binary tree (v101)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-102 — Check if balanced (v102)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-103 — Maximum depth (v103)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-104 — Minimum depth (v104)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-105 — Path sum (v105)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-106 — All root-to-leaf paths (v106)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-107 — Serialize and deserialize (v107)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-108 — Count complete tree nodes (v108)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-109 — Right side view (v109)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-110 — Zigzag level order (v110)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-111 — Flatten to linked list (v111)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-112 — Construct from preorder and inorder (v112)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-113 — Construct from postorder and inorder (v113)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-114 — Sum of left leaves (v114)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-115 — Binary tree maximum path sum (v115)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-116 — Vertical order traversal (v116)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-117 — Boundary traversal (v117)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-118 — Check subtree (v118)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-119 — Kth smallest in BST (v119)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-120 — Morris traversal (O(1) space) (v120)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-121 — Invert binary tree (v121)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-122 — Check if balanced (v122)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-123 — Maximum depth (v123)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-124 — Minimum depth (v124)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-125 — Path sum (v125)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-126 — All root-to-leaf paths (v126)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-127 — Serialize and deserialize (v127)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-128 — Count complete tree nodes (v128)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-129 — Right side view (v129)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-130 — Zigzag level order (v130)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-131 — Flatten to linked list (v131)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-132 — Construct from preorder and inorder (v132)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-133 — Construct from postorder and inorder (v133)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-134 — Sum of left leaves (v134)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-135 — Binary tree maximum path sum (v135)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-136 — Vertical order traversal (v136)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-137 — Boundary traversal (v137)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-138 — Check subtree (v138)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-139 — Kth smallest in BST (v139)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-140 — Morris traversal (O(1) space) (v140)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-141 — Invert binary tree (v141)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-142 — Check if balanced (v142)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-143 — Maximum depth (v143)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-144 — Minimum depth (v144)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-145 — Path sum (v145)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-146 — All root-to-leaf paths (v146)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-147 — Serialize and deserialize (v147)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-148 — Count complete tree nodes (v148)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-149 — Right side view (v149)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-150 — Zigzag level order (v150)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-151 — Flatten to linked list (v151)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-152 — Construct from preorder and inorder (v152)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-153 — Construct from postorder and inorder (v153)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-154 — Sum of left leaves (v154)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-155 — Binary tree maximum path sum (v155)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-156 — Vertical order traversal (v156)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-157 — Boundary traversal (v157)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-158 — Check subtree (v158)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-159 — Kth smallest in BST (v159)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-160 — Morris traversal (O(1) space) (v160)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-161 — Invert binary tree (v161)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-162 — Check if balanced (v162)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-163 — Maximum depth (v163)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-164 — Minimum depth (v164)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-165 — Path sum (v165)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-166 — All root-to-leaf paths (v166)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-167 — Serialize and deserialize (v167)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-168 — Count complete tree nodes (v168)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-169 — Right side view (v169)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.16a-170 — Zigzag level order (v170)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.