The topic is from LeetCode
104. Maximum depth of binary tree
Other solutions or source code can be accessed: tongji4m3
description
Given a binary tree, find its maximum depth.
The depth of the binary tree is the number of nodes on the longest path from the root node to the farthest leaf node.
Explanation: A leaf node refers to a node without child nodes.
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回它的最大深度 3 。
Code
public int maxDepth(TreeNode root)
{
if(root==null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}