二叉树最大深度


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

posted @ 2023-01-31 15:53  小傻孩丶儿  阅读(14)  评论(0编辑  收藏  举报