当前位置:网站首页>111. minimum depth of binary tree

111. minimum depth of binary tree

2022-06-11 14:23:00 @[toc] (directory)

https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/

 Insert picture description here

This inscription is written

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
    
    public int minDepth(TreeNode root) {
    
        // Determine the parameters of the recursive function 
        // Recursive termination condition 
        if(root == null){
    
            return 0;
        }

        int leftH = minDepth(root.left);
        int rightH = minDepth(root.right);
        // If one of the left subtree or the right subtree is empty 
        // if(!(root.right==null)||!(root.left==null)){
    
        // return 1+Math.max(leftH,rightH);
        // } 
        if(root.left==null&&root.right!=null){
    
            return 1+rightH;
        }
        if(root.left!=null&&root.right==null){
    
            return 1+leftH;
        }
		// When the left and right subtrees are empty , That's the leaf node , The recursive termination condition is triggered ,
		// return 0
        return 1+Math.min(leftH,rightH);    // The master , I understand ,
    }
}

 Insert picture description here

 Insert picture description here

原网站

版权声明
本文为[@[toc] (directory)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012047166914.html