二叉树简单 LeetCode111. 二叉树的最小深度 NC234 二叉树的最小深度

111. 二叉树的最小深度

描述

给定一颗节点数为N的二叉树,求其最小深度。最小深度是指树的根节点到最近叶子节点的最短路径上节点的数量。
(注:叶子节点是指没有子节点的节点。)

import java.util.*;
public class Solution {
    
    
    public int run (TreeNode root) {
    
    
        if(root == null){
    
    
            return 0;
        }
        int l = run(root.left);
        int r = run(root.right);
        if(l == 0){
    
    
            return r+1;
        }
        if(r == 0){
    
    
            return l+1;
        }
        return Math.min(l,r)+1;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43260719/article/details/121916323
今日推荐