-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathright-side-view-of-binary-tree.js
75 lines (59 loc) · 1.44 KB
/
right-side-view-of-binary-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Binary Tree Right Side View (https://leetcode.com/problems/binary-tree-right-side-view/)
- Given the root of a binary tree, return an array of the values of the nodes you can see when you stand on the right side of the tree
- Nodes should be ordered from top to bottom.
Constraints:
- Return an empty array when the tree is empty
*/
// ---- Solution 1 ----
/*
DFS - reverse Preorder DFS (Node, Right, Left)
*/
const dfs = function (node, level, ans) {
if (!node) return ans;
if (ans[level] === undefined) {
ans[level] = node.val;
}
dfs(node.right, level + 1, ans);
dfs(node.left, level + 1, ans);
return ans;
};
const rightSideView = function (root) {
return dfs(root, 0, []);
};
// ---- Space and Time Complexity 1 ----
/*
Time: O(n)
Space: O(n)
*/
// ---- Solution 2 ----
/*
BFS - Save the value of the last node at the same level
*/
const rightSideView = function (root) {
if (!root) return [];
const queue = [root];
const ans = [];
while (queue.length) {
let levelNodesCount = queue.length;
while (levelNodesCount > 0) {
const currentNode = queue.shift();
if (levelNodesCount === 1) {
ans.push(currentNode.val);
}
if (currentNode.left) {
queue.push(currentNode.left);
}
if (currentNode.right) {
queue.push(currentNode.right);
}
levelNodesCount--;
}
}
return ans;
};
// ---- Space and Time Complexity 2 ----
/*
Time: O(n)
Space: O(n)
*/