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 <--- / \ 2 3 <--- \ \ 5 4 <---
You should return [1, 3, 4]
.
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left; 6 * public TreeNode right; 7 * public TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public IList<int> RightSideView(TreeNode root) { 12 var queue = new Queue<TreeNode>(); 13 var result = new List<int>(); 14 15 if (root == null) return result; 16 17 queue.Enqueue(root); 18 19 int count = 1; 20 while (queue.Count > 0) 21 { 22 for (int i = 0; i < count; i++) 23 { 24 var n = queue.Dequeue(); 25 if (n.left != null) queue.Enqueue(n.left); 26 if (n.right != null) queue.Enqueue(n.right); 27 28 if (i == count - 1) result.Add(n.val); 29 } 30 31 count = queue.Count; 32 } 33 34 return result; 35 } 36 }