using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return int整型二维数组 */ public List<List<int>> Print (TreeNode pRoot) { List<List<int>> res = new List<List<int>>(); Queue<TreeNode> que = new Queue<TreeNode>(); if(pRoot != null) que.Enqueue(pRoot); bool needReverse = false; while(que.Count != 0){ List<int> curList = new List<int>(); for(int i = que.Count; i > 0; i--){ TreeNode cur = que.Dequeue(); curList.Add(cur.val); if(cur.left != null) que.Enqueue(cur.left); if(cur.right != null) que.Enqueue(cur.right); } if(needReverse) curList.Reverse(); needReverse = !needReverse; res.Add(curList); } return res; } }