确定一棵二叉树的深度。
- 时间复杂度O(N),空间复杂度O(H)
- Runtime: 80 ms, faster than 94.72%
- Memory Usage: 41.4 MB, less than 79.98%
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if (root === null) return 0
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1
};