力扣104. 二叉树的最大深度

104. 二叉树的最大深度

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public int maxDepth(TreeNode root) {
12         if(root == null){
13             return 0;
14         }
15         // 递归计算左右子树的高度,树的高度等于左右子树的最大高度加一
16         int leftDep = maxDepth(root.left);
17         int rightDep = maxDepth(root.right);
18         return Math.max(leftDep, rightDep) + 1;
19     }
20 }

猜你喜欢

转载自www.cnblogs.com/hi3254014978/p/12944738.html