C# Solution


  • 0
    A
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left;
     *     public TreeNode right;
     *     public TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public IList<IList<int>> FindLeaves(TreeNode root) 
        {
            IList<IList<int>> result = new List<IList<int>> ();
            
            LeavesLevel(root, result);
            
            return result;
        }
        
        public int LeavesLevel(TreeNode root, IList<IList<int>> result)
        {
            if (root == null)
            {
                return -1;
            }
            
            int leftLevel = LeavesLevel(root.left, result);
            int rightLevel = LeavesLevel(root.right, result);
            
            int level = Math.Max(leftLevel, rightLevel) + 1;
            
            if (result.Count() < level + 1)
            {
                result.Add(new List<int>());
            }
            
            result[level].Add(root.val);
            
            return level;
        }
    }
    

Log in to reply
 

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