Easy DFS Solution - Beats 91% C#


  • 0
    public class Solution
    {
        public IList<int> LargestValues(TreeNode root)
        {
            IList<int> Max = new List<int>();
            LargestValues(root, 0, Max);
            return Max;
        }
        private void LargestValues(TreeNode root, int level, IList<int> max)
        {
            if (root == null) return;
            if (max.Count == level)
                max.Add(root.val);
            else
                max[level] = Math.Max(max[level], root.val);
    
            LargestValues(root.left, level + 1, max);
            LargestValues(root.right, level + 1, max);
    
        }
    }
    

Log in to reply
 

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