[LeetCode] 199. Binary Tree Right Side View

来源:互联网 发布:淘宝客 click url 编辑:程序博客网 时间:2024/05/12 18:06
/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public List<Integer> rightSideView(TreeNode root) {        List<Integer> list = new ArrayList<>();        if(root == null) return list;        helper(list, 0, root);        return list;    }        private void helper(List<Integer> list, int depth, TreeNode root){        if(root == null){            return;        }        if(depth == list.size()){            list.add(root.val);        }        helper(list, depth + 1, root.right);        helper(list, depth + 1, root.left);    }}

0 0
原创粉丝点击