A Short C# Recursive Solution


  • 0
    L
    public IList<int> RightSideView(TreeNode root) {
        IList<int> result = new List<int>();
        dfs(result, 1, root);
        return result;
    }
    
    private void dfs(IList<int> result, int curDepth, TreeNode root){
        if(root == null) return;
        if(curDepth > result.Count) result.Add(root.val);
        if(root.right != null) dfs(result, curDepth + 1, root.right);
        if(root.left != null) dfs(result, curDepth + 1, root.left);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.