您的位置: 首页 > IT文章 > Java实现LeetCode 111. Minimum Depth of Binary Tree Java实现LeetCode 111. Minimum Depth of Binary Tree 分类: IT文章 • 2022-05-26 19:58:06 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public int minDepth(TreeNode root) { if(root == null){ return 0; } if((root.left == null) && (root.right == null)){ return 1; } int min_depth = Integer.MAX_VALUE; if(root.left != null){ min_depth = Math.min(minDepth(root.left),min_depth); } if(root.right != null){ min_depth = Math.min(minDepth(root.right),min_depth); } return min_depth+1; } } /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public int minDepth(TreeNode root) { if(root == null){ return 0; } if((root.left == null) && (root.right == null)){ return 1; } int min_depth = Integer.MAX_VALUE; if(root.left != null){ min_depth = Math.min(minDepth(root.left),min_depth); } if(root.right != null){ min_depth = Math.min(minDepth(root.right),min_depth); } return min_depth+1; } }