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]
.
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
public class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> list = new ArrayList<Integer>(); rightView(list, root, 0); return list; } public void rightView(List<Integer> list, TreeNode root, int depth){ if(root == null) return ; if(list.size() == depth) list.add(root.val); rightView(list,root.right,depth+1); rightView(list,root.left,depth+1); } }