Example #1
0
        public static void Helper(LeetCode102TreeNode root, int level)
        {
            if (root == null)
            {
                return;
            }
            if (level == ans.Count)
            {
                ans.Add(new List <int>());
            }
            ans[level].Add(root.val);

            if (root.left != null)
            {
                Helper(root.left, level + 1);
            }
            if (root.right != null)
            {
                Helper(root.right, level + 1);
            }
        }
Example #2
0
 public static IList <IList <int> > LevelOrder(LeetCode102TreeNode root)
 {
     Helper(root, 0);
     return(ans);
 }
Example #3
0
 public LeetCode102TreeNode(int val = 0, LeetCode102TreeNode left = null, LeetCode102TreeNode right = null)
 {
     this.val   = val;
     this.left  = left;
     this.right = right;
 }