Efficient and elegant C# 10 line solution


  • 0
    A

    public class Solution {
    public IList<int> RightSideView(TreeNode root)
    {
    List<int> viewList = new List<int>();
    RightSideViewInternal(root, viewList,1);
    return viewList;
    }
    public void RightSideViewInternal(TreeNode root, List<int> viewList, int level)
    {
    if (root == null) return;
    if (viewList.Count < level) viewList.Add(root.val);
    RightSideViewInternal(root.right,viewList,level+1);
    RightSideViewInternal(root.left, viewList, level + 1);
    }
    }


Log in to reply
 

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