Trees, traversal, and recursive structure
targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no children.
Key phrase: "root-to-leaf path". The path must start at the root and end at a leaf. A leaf has no left or right child.
You can't stop at an internal node even if the sum matches. You must reach the bottom.
Trees are recursive structures. Each subtree is itself a tree. Use recursion (DFS) to walk from root to leaves.
At each node, subtract its value from the target. Pass the reduced target down. At a leaf, check if the remaining target is zero.
⚙ Knowledge Hub: Using Trees ⚙ Knowledge Hub: Recursionnull, return false (fell off the tree)targetSumtargetSum == 0true if either side returns truetargetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of node values.
Same rules as Path Sum, but now you need all valid paths, not just whether one exists. You must collect the actual node values along each valid path.
There can be multiple valid paths or none at all.
Use DFS, same as Path Sum, but this time keep a list of the nodes you've visited so far. Each time you move to a child, add that node to the list. Each time you return from a child, remove the last item so the list is ready for the next branch.
When you reach a valid leaf, save a copy of that list to your results.
⚙ Knowledge Hub: Using Trees ⚙ Knowledge Hub: Recursionresult list (collects valid paths) and a path list (current path being built)null, return immediatelypath and subtract from remaining0, append a copy of path to resultpath (undo it before returning)0.
Height counts edges, not nodes. A single node has height 0 (zero edges). An empty tree (null) has height -1.
You need the longest path from root down to any leaf.
The height of a node depends on the heights of its children. This is a natural recursive problem. Get the height of left subtree, get the height of right subtree, take the max, add 1.
⚙ Knowledge Hub: Using Trees ⚙ Knowledge Hub: Recursionnull, return -1 (no edges below a missing node)leftHeight = height(node.left)rightHeight = height(node.right)1 + max(leftHeight, rightHeight)Common mistake: only checking if left.data < node.data < right.data for direct children. That's not enough.
Every node in the left subtree must be less than the root, not just the immediate left child. Same for the right side. The constraint flows down through the entire subtree.
Pass a valid range (min, max) down as you recurse. Each node must fall strictly within its allowed range. Going left tightens the upper bound. Going right tightens the lower bound.
⚙ Knowledge Hub: Using Trees ⚙ Knowledge Hub: Recursionvalid(node, low, high) that checks if a subtree is valid within range (low, high)low = -infinity, high = +infinitynull, return true (an empty tree is a valid BST)node.data <= low or node.data >= high, return falsevalid(node.left, low, node.data) (left children must be less than current)valid(node.right, node.data, high) (right children must be greater than current)trueIn-order traversal visits: left subtree, then current node, then right subtree. For a valid BST, this produces values in strictly increasing order.
If at any point the current value is not greater than the previous one, the tree is not a valid BST.
node.data > prev? If not, return falseprev = node.data and continuetrue