Balanced Binary Tree:Height or -1 on the Way Up, Not Max-Depth Twice
Height-balanced means |lh - rh| <= 1 at every node. Nested maxDepth at each node is correct and quadratic; one post-order walk can return the height or fail.
Read More23 questions
Height-balanced means |lh - rh| <= 1 at every node. Nested maxDepth at each node is correct and quadratic; one post-order walk can return the height or fail.
Read MoreThe longest path through a node is leftHeight + rightHeight, counted in edges. Nested height walks are correct and quadratic; empty and a lone node are both 0.
Read MoreInverting a tree is a left/right swap at every node. A second tree of copied values looks mirrored and is not the tree callers still hold.
Read MoreDepth is 1 + max(left, right). Enumerating every root-to-leaf path is correct and extra; empty is 0, a lone node is 1, and this prompt counts nodes not edges.
Read MoreRecurse with target minus node.val. True only when a leaf's remaining is 0. Returning true because an internal node equals the target is the trap.
Read MoreTwo trees match only if a paired walk sees the same structure and values. Serializing both then comparing is correct and extra; a left child versus a right child is already false.
Read MoreInserting a sorted array in order builds a right spine. The mid of each remaining slice is the root: BST order and height-balance from the same cut.
Read MoreA match can hang off any node. Comparing only the two roots is Same Tree; call that check here, then try the children.
Read MoreA tree is symmetric only if a paired walk sees mirrors. Inverting a copy then running Same Tree is correct and extra; left with left is the wrong pairing.
Read MoreConstructor stacks the left spine only. next() pops one visit and pushes the left spine of the right child — materializing every key up front is the brute that pays O(n) before the first call.
Read MoreA node is good if it is at least the max on the path from the root. Scanning that path at every node is quadratic; checking only the parent is not enough.
Read MoreA prev pointer in reverse post-order hangs right as next. Copying values into a list then building a new chain is not the tree callers still hold.
Read MoreInorder of a BST is already rank order. Materializing every key then indexing k-1 is correct and throws away the early exit — return on the kth visit.
Read MoreNo BST order, so both children get searched. If both return a hit, this node is the LCA — walking by key comparison is the other problem.
Read MoreBoth keys smaller → left, both larger → right, split → this node. Path lists are correct and ignore the order you already paid for.
Read MoreSnapshot q.size() and poll that many nodes — that is one level. A second queue is extra furniture; DFS into depth buckets is a follow-up, not the default walk.
Read MoreCount downward paths that sum to target by hashing prefixes on the current root-to-node walk. Nested starts are honest and quadratic; skip the decrement and siblings steal counts.
Read MoreThe next unused preorder value is always the root; its inorder index is the left/right cut. Scanning inorder for every root still rebuilds the tree — in quadratic time.
Read MoreSnapshot q.size() and record only the last poll of that drain. Walking only right children misses a left-subtree node that peeks out as the sole node on a deeper floor.
Read MoreA tree is a BST only if every node sits inside the range its ancestors allow. An inorder dump that looks sorted can still hide a violation if you check the wrong thing — and a left-child-only check is not enough.
Read MoreSnapshot size, collect the floor left to right, reverse the odd ones. A pair of ArrayDeques, or polling either end, are variants, not a second BFS.
Read MoreReturn node + max(0, leftGain, rightGain) — one child, because a path to the parent cannot fork. Start the global max at Integer.MIN_VALUE because all-negative trees exist.
Read MoreLevel-order that drops holes cannot round-trip. Encode missing children as string tokens and never offer null to ArrayDeque; a 2^h slot dump wastes a spine.
Read MoreRepresentation and operations — not the problem set.