# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0;
elif not root.left:
return self.maxDepth(root.right) + 1;
elif not root.right:
return self.maxDepth(root.left) + 1;
else:
return max(self.maxDepth(root.left),self.maxDepth(root.right)) + 1;
"""
TESTCASES:
Input:
[]
[1]
[1,1]
[1,null,2]
[1,2,3,null,null,1,2,1,null,null,null,1]
Output:
0
1
2
2
5
"""
/**
* 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 maxDepth(TreeNode root) {
if(root == null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}