0104. Maximum Depth of Binary Tree
Previous0103. Binary Tree Zigzag Level Order TraversalNext0105. Construct Binary Tree from Preorder and Inorder Traversal
Last updated
Last updated
**Input:** root = [3,9,20,null,null,15,7]
**Output:** 3**Input:** root = [1,null,2]
**Output:** 2**Input:** root = []
**Output:** 0**Input:** root = [0]
**Output:** 1/**
* 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) {
int depth = 0;
if (root == null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return left > right ? 1 + left : 1 + right;
}
}