题目链接:https://leetcode.com/problems/maximum-depth-of-binary-tree/
思路:
深度遍历,保存最大值。
AC 0ms 100% Java:
/**
* 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) {
return helper(root,0);
}
public int helper(TreeNode root,int depth){
if(root==null)
return depth;
return Math.max(helper(root.left,depth+1),helper(root.right,depth+1));
}
}
博客给出二叉树最大深度题目的链接,解题思路是采用深度遍历并保存最大值,还展示了AC的Java代码,用时0ms,击败100%。

1408

被折叠的 条评论
为什么被折叠?



