-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path100-binary-tree-right-side-view.cs
More file actions
39 lines (35 loc) · 1.21 KB
/
100-binary-tree-right-side-view.cs
File metadata and controls
39 lines (35 loc) · 1.21 KB
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public IList<int> RightSideView(TreeNode root) {
var result = new List<int>();
if(root is null) return result;
var deq = new LinkedList<TreeNode>();
deq.AddLast(root);
// do this until the queue is empty
while(deq.Count > 0){
var n = deq.Count; // store the current size of the deq (in this level)
TreeNode lastNonNullInLevel = null;
for(var i = 0; i < n; i++){
TreeNode trav = deq.First.Value;
if(trav is not null) lastNonNullInLevel = trav;
if(trav.left is not null) deq.AddLast(trav.left);
if(trav.right is not null) deq.AddLast(trav.right);
deq.RemoveFirst();
}
if(lastNonNullInLevel is not null) result.Add(lastNonNullInLevel.val);
}
return result;
}
}