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 root TreeNode类 * @return int整型二维数组 */ public List<List<int>> levelOrderBottom (TreeNode root) { // write code here if (root == null) return null; List<List<int>> lslsN = new List<List<int>>(); lslsN.Add(new List<int>() { root.val }); GDBL(root, ref lslsN, 1); for (int nIndex = lslsN.Count - 1; nIndex >= 0; nIndex--) { if (lslsN[nIndex].Count != 0) continue; lslsN.RemoveAt(nIndex); } lslsN.Reverse(); return lslsN; } public static void GDBL(TreeNode root, ref List<List<int>> lslsN, int nCurH) { // write code here if (root.left == null && root.right == null) return; List<int> lsN = new List<int>(); lslsN.Add(lsN); if (root.left != null) { lslsN[nCurH].Add(root.left.val); GDBL(root.left, ref lslsN, nCurH + 1); } if (root.right != null) { lslsN[nCurH].Add(root.right.val); GDBL(root.right, ref lslsN, nCurH + 1); } } }