Skip to content

Commit f1b4436

Browse files
committed
binary tree - dfs recu O(n) O(n)
1 parent 5d1dfb9 commit f1b4436

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
14+
// p: root of bi tree
15+
// r: num
16+
17+
var maxDepth = function (root) {
18+
if (!root) return 0;
19+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
20+
};

0 commit comments

Comments
 (0)