-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary-tree-right-side-view.js
144 lines (128 loc) · 3.1 KB
/
binary-tree-right-side-view.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Source: https://leetcode.com/problems/binary-tree-right-side-view/
* Tags: [Tree,Depth-first Search,Breadth-first Search]
* Level: Medium
* Title: Binary Tree Right Side View
* Auther: @imcoddy
* Content: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
*
*
* For example:
* Given the following binary tree,
*
* 1
*
*
* You should return [1, 3, 4].
*
*
* Credits:Special thanks to @amrsaqr for adding this problem and creating all test cases.
*/
/**
* Definition for binary tree
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @returns {number[]}
*/
/**
* Memo: Breadth-first Search
* Runtime: 149ms
* Rank: A
*/
var rightSideView = function(root) {
if (!root) {
return [];
}
var queue = [root];
var result = [];
var level_count = 1;
while (queue.length) {
var next_level_count = 0;
var node;
for (var i = 0; i < level_count; i++) {
node = queue.shift();
if (node.left) {
next_level_count++;
queue.push(node.left);
}
if (node.right) {
next_level_count++;
queue.push(node.right);
}
}
level_count = next_level_count;
result.push(node.val);
}
return result;
};
/**
* Memo: Use BFS to do a level traversal. Save each level in a queue, and add last element of that level into result.
* Complex: O(n)
* Runtime: 156ms
* Tests: 210 test cases passed
* Rank: A
*/
var rightSideView = function(root) {
if (!root) return [];
var result = [];
var queue = [root];
var count = 1;
do {
var new_count = 0;
for (var i = 0; i < count; i++) {
var node = queue.shift();
if (node.left) {
queue.push(node.left);
new_count++;
}
if (node.right) {
queue.push(node.right);
new_count++;
}
}
count = new_count;
result.push(node.val);
} while (queue.length);
return result;
};
/**
* Memo: Recursive solution. Traverse right child first, and only add to result if it is the first child of that level.
* Complex: O(n)
* Runtime: 164ms
* Tests: 210 test cases passed
* Rank: A
*/
var rightSideView = function(root) {
var deepest = 0;
var result = [];
function traverse(root, level) {
if (!root) return;
if (level >= deepest) {
deepest++;
result.push(root.val);
}
level++;
traverse(root.right, level);
traverse(root.left, level);
}
traverse(root, 0);
return result;
};
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
var node = new TreeNode(1);
var root = node;
node = new TreeNode(2);
root.right = node;
node = new TreeNode(3);
root.left = node;
node = new TreeNode(4);
root.left.left = node;
console.log(rightSideView(root));