首页 > 其他分享 >二叉树最大深度

二叉树最大深度

时间:2023-01-31 16:01:56浏览次数:30  
标签:right TreeNode 最大 val int 二叉树 深度 root left


//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)

标签:right,TreeNode,最大,val,int,二叉树,深度,root,left
From: https://www.cnblogs.com/xiaoshahai/p/17079457.html

相关文章