199. Binary Tree Right Side View

The problem of this solution can be BFS and DFS.

BFS is easy to understand.

The following is DFS solution:

    private List<Integer> res = new ArrayList<>();
    public List<Integer> rightSideView(TreeNode root) {
        helper(root, 0);
        return res;
    }
    
    private void helper(TreeNode root, int level){
        if(root==null)
            return;
        if(res.size()==level){
            res.add(root.val);
        }
        helper(root.right, level+1);
        helper(root.left, level+1);
    }

 

上一篇:elasticsearch-rest-high-level-client操作elasticsearch,一篇带你如入无人之境


下一篇:lead计算留存率