题解 | #从下到上打印二叉树#

从下到上打印二叉树

https://www.nowcoder.com/practice/ed982e032ff04d6a857b4cb4e6369d04

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);
        }
    }
}

全部评论

相关推荐

05-12 17:00
门头沟学院 Java
king122:你的项目描述至少要分点呀,要实习的话,你的描述可以使用什么技术,实现了什么难点,达成了哪些数字指标,这个数字指标尽量是真实的,这样面试应该会多很多,就这样自己包装一下,包装不好可以找我,我有几个大厂最近做过的实习项目也可以包装一下
点赞 评论 收藏
分享
05-12 16:04
已编辑
江西财经大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务