题解 | #二叉树的最小深度#

二叉树的最小深度

https://www.nowcoder.com/practice/6a7f40d7696d46f79c74c61179993be6

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 int run (TreeNode root) {
        // write code here
        return BFS(root);
    }

    public int BFS (TreeNode root) {
        // write code here
        if (root == null)
            return 0;
        if (root.left == null)
            return 1 + BFS(root.right);
        if (root.right == null)
            return 1 + BFS(root.left);
        int nLeft = 1 + BFS(root.left);
        int nRight = 1 + BFS(root.right);
        return Math.Min(nLeft, nRight);
    }
}

全部评论

相关推荐

02-08 20:56
已编辑
南京工业大学 Java
在等offer的比尔很洒脱:我也是在实习,项目先不说,感觉有点点小熟悉,但是我有点疑问,这第一个实习,公司真的让实习生去部署搭建和引入mq之类的吗,是不是有点过于信任了,我实习过的两个公司都是人家正式早搭好了,根本摸不到部署搭建的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务