111. 二叉树的最小深度(补8/21)
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
和找二叉树最大深度刚好相反
dfs递归遍历,找到最小深度
/**
 * 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 minDepth(root.right)+1; if(root.right==null&&root.left!=null) return minDepth(root.left)+1; return Math.min(minDepth(root.left),minDepth(root.right))+1; } }

 
                
             
         浙公网安备 33010602011771号
浙公网安备 33010602011771号