Example #1
0
        private static void Helper(LeetCode103TreeNode root, int level)
        {
            if (root == null)
            {
                return;
            }

            if (level >= ans.Count)
            {
                var temp = new List <int>();
                temp.Add(root.val);

                ans.Add(temp);
            }
            else
            {
                if (level % 2 == 0)
                {
                    ans[level].Add(root.val);
                }
                else
                {
                    ans[level].Insert(0, 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> > ZigzagLevelOrder(LeetCode103TreeNode root)
 {
     Helper(root, 0);
     return(ans);
 }
Example #3
0
 public LeetCode103TreeNode(int val = 0, LeetCode103TreeNode left = null, LeetCode103TreeNode right = null)
 {
     this.val   = val;
     this.left  = left;
     this.right = right;
 }