1.求二叉树最大深度
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
}
2.求二叉树最小深度
int minDepth(TreeNode *root) {
if(root == NULL)
return false;
if(root->left == NULL) return minDepth(root->right) + 1;
if(root->right == NULL) return minDepth(root->left) + 1;
int leftDepth = minDepth(root->left);
int rightDepth = minDepth(root->right);
return leftDepth < rightDepth ? (leftDepth + 1) : (rightDepth + 1);
}
浙公网安备 33010602011771号