Day13-BinaryTree01
144. Binary Tree Preorder Traversal
Node:
- Preorder: mid, left, right
- Iterate: push the right node, then push the left node
Time Complexity && Space Complexity
Recursive
- Time Complexity: O(n)
- Space Complexity: O(n)
1
2
3
4
5
6
7
8
9
10
11var preorderTraversal = function (root) {
const ans = [];
function traversal(root) {
if (!root) return;
ans.push(root.val);
traversal(root.left);
traversal(root.right);
}
traversal(root);
return ans;
};Iterate
- Time Complexity: O(n)
- Space Complexity: O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16var preorderTraversal = function (root) {
if (!root) return [];
const stack = [root];
const ans = [];
while (stack.length > 0) {
const node = stack.pop();
ans.push(node.val);
if (node.right) {
stack.push(node.right);
}
if (node.left) {
stack.push(node.left);
}
}
return ans;
};
145. Binary Tree Postorder Traversal
Node:
- Postorder: left, right, mid
- Do preorder first, mid, right, left, means push the [left, right], then pop [right, left]
- then reverse the ans
Time Complexity && Space Complexity
Recursive
- Time Complexity: O(n)
- Space Complexity: O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15var postorderTraversal = function (root) {
const ans = [];
function traversal(root) {
if (!root) return;
if (root.left) {
traversal(root.left);
}
if (root.right) {
traversal(root.right);
}
ans.push(root.val);
}
traversal(root);
return ans;
};Iterate
- Time Complexity: O(n)
- Space Complexity: O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16var postorderTraversal = function (root) {
if (!root) return [];
const stack = [root];
const ans = [];
while (stack.length > 0) {
const node = stack.pop();
ans.push(node.val);
if (node.left) {
stack.push(node.left);
}
if (node.right) {
stack.push(node.right);
}
}
return ans.reverse();
};
94. Binary Tree Inorder Traversal
Node:
- left, mid, right
- Iterate
- Push the root, root.left, root.left.left until the leaf
- when reach the leaf, pop the node from stack
- check the popNode.right
Time Complexity && Space Complexity
Recursive
- Time Complexity: O(n)
- Space Complexity: O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15var inorderTraversal = function (root) {
const ans = [];
function traversal(root) {
if (!root) return;
if (root.left) {
traversal(root.left);
}
ans.push(root.val);
if (root.right) {
traversal(root.right);
}
}
traversal(root);
return ans;
};Iterate
- Time Complexity: O(n)
- Space Complexity: O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17var inorderTraversal = function (root) {
if (!root) return [];
const stack = [];
const ans = [];
let curr = root;
while (stack.length > 0 || curr) {
if (curr) {
stack.push(curr);
curr = curr.left;
} else {
curr = stack.pop();
ans.push(curr.val);
curr = curr.right;
}
}
return ans;
};
Binary Tree Depth:
Node:
- Use queue, and use length for loop
Time Complexity && Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(n)
102. Binary Tree Level Order Traversal
1 | var levelOrder = function (root) { |
107. Binary Tree Level Order Traversal II
1 | var levelOrderBottom = function (root) { |
199. Binary Tree Right Side View
1 | var rightSideView = function (root) { |
637. Average of Levels in Binary Tree
1 | var averageOfLevels = function (root) { |
429. N-ary Tree Level Order Traversal
1 | var levelOrder = function (root) { |
515. Find Largest Value in Each Tree Row
1 | var largestValues = function (root) { |
116. Populating Next Right Pointers in Each Node
1 | var connect = function (root) { |
117. Populating Next Right Pointers in Each Node II
1 | var connect = function (root) { |
104. Maximum Depth of Binary Tree
- BFS, queue
1 | var maxDepth = function (root) { |
- Recursive
1 | var maxDepth = function (root) { |
111. Minimum Depth of Binary Tree
- BFS, queue
1 | var minDepth = function (root) { |
- Recursive
1 | var minDepth = function(root){ |
- Title: Day13-BinaryTree01
- Author: Guoyi
- Created at : 2024-09-17 07:42:02
- Updated at : 2024-12-07 03:58:41
- Link: https://guoyiwang.github.io/Algorithm/Day13-BinaryTree01/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments