ð 104. äºåæ çæ倧深床
2022幎10æ10æ¥
- algorithm
ð 104. äºåæ çæ倧深床
éŸåºŠ: ð
é®é¢æè¿°
解æ³
/**
* 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) {
// æè·¯ïŒ
// éåœ
return mySol(root);
}
private int mySol(TreeNode root) {
// éåœç»æ¢æ¡ä»¶
if(root == null) {
return 0;
}
int left = mySol(root.left);
int right = mySol(root.right);
return Math.max(left, right) + 1;
}
}