| Problem Names | Solution Writeups | Code |
|---|---|---|
| Invert Binary Tree |
Do DFS until reaching leaf node, then swap left and right. |
|
| Maximmum Depth of binary Tree |
While curr node has child, go as deep as possible and update max_depth. |
|
| Diameter of Binary Tree |
For each node, find its leftmost and rightmost depth, and record the largest diameter found so far. When all nodes have been traversed, the longest diameter will be found. |
|
| Balanced Binary Tree |
The condition for a balanced binary tree is that the abs(height[left] - height[right]) <=1. (Provided that height[leaf] = 0). To calculate height, height[node] = height[child] + 1. If any of the subtree of current node is not balanced, simply return false. |
|
| Same Tree |
Check if each node matches one another, and there is no extra node in any tree. |
|
| Subtree of Another Tree |
If current node == sub, check all sub trees. If not exactly matched, need to continue by searching through dfs(curr->left) || dfs(curr->right). |
|
| Lowest Common Ancestor of a BST |
Starting at root, if p->val < root->val and q->val > root->val, then the LCA will be root, else if both < root->val then LCA will be at root->left, so traverse root->left. Else traverse root->right. |
|
| Binary Tree Level Order Traversal |
Do BFS on tree. First push root to frontier, while frontier is not empty, for every node in frontier, pushes all its children to the frontier, and add current node to a vector. This is how to create level-order traversal. |
|
| Binary Tree Right Side View |
Create a stack to store visited nodes. We will push curr->left to the node first, then curr->right so that when we pop from the stack we will get the rightmost node. Also, we keep track of current max_level so that for each node, it must at least have level == max_level so that we will add it to the view. |
|
| Count Good Nodes in Binary Tree |
Do DFS, keep track of curr_max along the path, so if curr->val > curr_max, count++, else go explore its children. Must traverse all nodes to determine. |
|
| Validate Binary Search Tree |
Notice that by doing in-order traversal in BST, we will get the nodes in sorted order. Simply check if curr->val > prev->val, if not return false. This optimizes space and time. |
|
| k-th Smallest Element in a BST |
Construct in-order traversal, stop when the size == k. |
|
| Construct Binary Tree From Preorder and Inorder Traversal |
Preorder guarantees root at 0 indexed, then we try to find root in inorder, partition its left to root->left and right to root->right. In pre-order, the next mid elements will be in the left, and the remaining will be in the right, where mid = index of inorder where its value = preorder[0]. |
|
| Binary Tree Maximum Path Sum |
There are two possibilities to get path sum, one is max_path(curr->left) + curr->val + max_path(curr->right), one is parent(curr) + curr->val + max(max_path(curr->left), max_path(curr->right)). Need to check both path sum and only store the maximum path sum to the global variable. |
|
| Serialize and Deserialize Binary Tree |
From a binary tree, create its pre-order traversal and then store as a string (each node can be separated by whitespace, NULL need to be represented as N or smth else). Then from the string, we can construct the binary tree back, since we know the root, and that for each node, we know if it contains children or is just a leaf node. |