Yifans_Z's Blog.

LeetCode Maximum Depth of Binary Tree 104

字数统计: 130阅读时长: 1 min
2016/08/23 Share

104. Maximum Depth of Binary Tree
  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.

自己的解法

自己想到应该用递归、为空的返回 0,不为空的返回 1,递归累加;但是有两个点这么判断呢?其实很容易取两数字的 MAX

public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}

参考:
104 Maximum Depth of Binary Tree | Data Structure and algorithm analysis

CATALOG
  1. 1. 自己的解法