104. Maximum Depth of Binary Tree
¶
- Time: $O(n)$
- Space: $O(1)$
1 2 3 4 5 6 7 8 | |
1 2 3 4 5 6 7 | |
1 2 3 4 5 | |
var maxDepth = function(root) {
if (!root) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
};
1 2 3 4 5 6 7 8 | |
1 2 3 4 5 6 7 | |
1 2 3 4 5 | |
var maxDepth = function(root) {
if (!root) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
};