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);
    }

 

posted on 2022-02-09 07:57  阳光明媚的菲越  阅读(20)  评论(0编辑  收藏  举报