199. Binary Tree Right Side View
Input:
[1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:
1 <---
/ \
2 3 <---
\ \
5 4 <---/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
preorder_reverse(root, result, 0);
return result;
}
public void preorder_reverse(TreeNode cur, List<Integer> result, int curDepth){
if (cur == null) return;
if (result.size() == curDepth) result.add(cur.val);
preorder_reverse(cur.right, result, curDepth + 1);
preorder_reverse(cur.left, result, curDepth + 1);
}
}Last updated
Was this helpful?