LeetCode-104
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example
Input: Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
Output: 3
Solution
Java
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class Solution {
List<Integer> inorder=new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
if (root==null)
return inorder;
inorderTraversal(root.left);
inorder.add(root.val);
inorderTraversal(root.right);
return inorder;
}
}
题目描述:
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
总结:
执行用时1 ms,在所有 Java 提交中击败了97.43%的用户。
内存消耗37 MB,在所有 Java 提交中击败了54.59%的用户。
本题之后会用迭代和C语言补充。最近做的都是树和图的基本算法题。

443

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



