/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(root==null){
return 0;
}
int lefdep=this.maxDepth(root.left);
int rigdep=this.maxDepth(root.right);
if(lefdep>rigdep){
return lefdep+1;
}
else{
return rigdep+1;
}
}
}
Maximum Depth of Binary Tree
最新推荐文章于 2025-05-05 16:31:47 发布