/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
//如果求最小就把结果初始化为最大,反之亦然
public int ans=Integer.MIN_VALUE;
public void robot(TreeNode root,int curDepth){
if(root==null) return ;
if(root.left==null && root.right==null){
ans = Math.max(ans ,curDepth);
}
if(root.left!=null)
robot(root.left,curDepth+1);
if(root.right!=null)
robot(root.right,curDepth+1);
}
public int maxDepth(TreeNode root) {
if(root==null) return 0;
robot(root,1);
return ans;
}
}
LeetCode104:Maximum Depth of Binary Tree
最新推荐文章于 2021-12-01 10:33:50 发布