题解 | 二叉树的最小深度

二叉树的最小深度

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

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    public int run (TreeNode root) {
        // write code here
        if (null==root){
            return 0;
        }

        if(null==root.left){
            return run(root.right)+1;
        }

        if(null==root.right){
            return run(root.left)+1;
        }

        return Math.min(run(root.left),run(root.right))+1;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务