Easy C# Solution Accepted


  • 0
    
    public class Solution {
        public IList<IList<int>> LevelOrder(TreeNode root) {
            
            IList<IList<int>> res = new List<IList<int>>();
            if(root == null) return res;
            return TraverseLeaf(root, 0, res);
        }
        
        public IList<IList<int>> TraverseLeaf(TreeNode root,int level, IList<IList<int>> res)
        {
             if(res.Count <= level) res.Insert(level, new List<int>());
             res[level].Add(root.val);
             
            if(root.left != null){TraverseLeaf(root.left, level+1, res);}
            if(root.right != null){TraverseLeaf(root.right, level+1, res);}
            return res;
        }
    }
    

Log in to reply
 

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