0
点赞
收藏
分享

微信扫一扫

二叉树最大深度

人间四月天i 2023-02-05 阅读 166

//leetcode submit region begin(Prohibit modification and deletion)
/**
* 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 maxDepth(TreeNode root) {
if(root == null){
return 0;
}

int leftMaxDepth = maxDepth(root.left);
int rightMaxDepth = maxDepth(root.right);
int res = Math.max(leftMaxDepth, rightMaxDepth)+1;
return res;
}
}
//leetcode submit region end(Prohibit modification and deletion)

不恋尘世浮华,不写红尘纷扰



举报

相关推荐

0 条评论