/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return depth(root);
}
public int depth(TreeNode root) {
if (root == null) return 0;
int lh = depth(root.left);
int rh = depth(root.right);
return lh > rh ? lh + 1 : rh + 1;
}
}
二叉树的深度
最新推荐文章于 2021-12-18 15:59:21 发布