Solutions: https://www.youtube.com/watch?v=as1f6PJmbAE&t=50s
Level Order Traversal (Breadth First Traversal)
102. Binary Tree Level Order Traversal
Leetcode: https://leetcode.com/problems/binary-tree-level-order-traversal/description/
Solution
[1] My solution: Use two Queue
s to seperately store values on different depth of the tree
[2] DFS solution: construct [[], [], []] first, then use dep to track depth of the tree; Fill in the empty [] when traversal
1 | // DFS Solution |
103. Binary Tree Zigzag Level Order Traversal
Leetcode: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/
[Solution] Use level % 2 == 0
to decide how to insert value into sublists
1 | class Solution { |
107. Level Order travesal II, bottom up
Leetcode: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/
1 | class Solution { |
199. Binary Tree Right Side View
Leetcode: https://leetcode.com/problems/binary-tree-right-side-view/description/
[dfs solution]
The result list should return all the rightmost nodes in each level of the tree. Since it should recognize ‘level’, we should set up a variable to keep track of the level. Once the res.length == level
, we append the root.val
and forward to the next level.
This solutions uses right level oriented preorder traversal. Will check right child node first, if it’s null
, return and go to left child.
1 | class Solution { |
Raised question about dfs(root.right, res, depth++)
at StackOverflow: https://stackoverflow.com/questions/53018302/whats-the-difference-between-depth-1-and-depth-in-this-dfs-recursion-functi
[bfs solution]
Depth First Traversal
Introduction: https://www.youtube.com/watch?v=cIoGuSCU14A
100. Same Tree(easy)
[Recursive solution] Just need to check if left tree == right tree
1 | class Solution { |
[Iterative Solution] Use a stack to keep comparing left child tree and right child tree.
1 | class Solution { |
101. Symmetric Tree
[Solution] Construct a new function that allows two parameters, left, and right, compare left.right and right.left tree.
1 | class Solution { |
110. Balanced Binary Tree
[Solution] TBA
Pre-order Traversal
105. Construct Binary Tree from Preorder and Inorder Traversal
Leetcode: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
[Solution-recursion]
Key: There is not duplicates in the tree. Thus, the root will be the first element in the pre-order array, and you can find the index of the root in the in-order array. The subarray on the left of the root in the in-order array will be the left sub-tree, vice versa.
1 | 1 |
1 | class Solution { |
1 | def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode: |
An elegant python solution:1
2
3
4
5
6
7def buildTree(self, preorder, inorder):
if inorder:
ind = inorder.index(preorder.pop(0))
root = TreeNode(inorder[ind])
root.left = self.buildTree(preorder, inorder[0:ind])
root.right = self.buildTree(preorder, inorder[ind+1:])
return root
114. Flatten Binary Tree to LinkedList
Leetcode: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/description/
[Solution] Use a Dummy Node prev
to keep track of nodes in each level.
1 | class Solution { |
297. Serialize and Deserialize Binary Tree
Leetcode: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
Pretty good Video: https://www.youtube.com/watch?v=suj1ro8TIVY
1 | from collections import deque |
1110. Delete Nodes And Return Forest
Leetcode: https://leetcode.com/problems/delete-nodes-and-return-forest/
1 | def delNodes(self, root, to_delete): |
In-order Traversal
Iterative In-order Traversal
Introduction: https://www.youtube.com/watch?v=VsxLHGUqAKs
- Create an empty stack S
- Initialize current node as root
- Push the current node to S and set
current = current.left
untilcurrent == null
- if current is null and stack is not empty
a. pop the top item from stack
b. print the popped item, set current = popped_item.right
c. go to step 3 - If
current == null
and stack is empty finish iteration
1 | public void inorderTraverse(TreeNode root) { |
Visit left
first, then check if current node has a right
child, if so, visit right
child.
1 | def inorderTraversal(self, root: TreeNode) -> List[int]: |
106. Construct Binary Tree from Inorder and Postorder Traversal
Solution same as 105.
Leetcode: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/
94.Inorder Traversal
Leetcode: https://leetcode.com/problems/binary-tree-inorder-traversal/description/
Solution: Generic Class Unit
1 | class Solution { |
Post-order Traversal
226. Invert Binary Tree(easy)
Leetcode: https://leetcode.com/problems/invert-binary-tree/description/
Solution: just reverse the left child and right child
1 | class Solution { |
110. Balanced Binary Tree
Leetcode: https://leetcode.com/problems/balanced-binary-tree/description/
Solution: Test if the abs(left - right) > 1
1 | // Fastest solution |
145. Postorder Traversal
Leetcode: https://leetcode.com/problems/binary-tree-postorder-traversal/description/
Solution: Use generic class Unit, set operation
1 | class Solution { |
297. Serialize and Deserialize Binary Tree (Hard)
Leetcode: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/
[Solution]
- Use pre-order traversal to push nodes in an array
- Use pre-order traversal to construct the tree back from string
1 | /** |