Skip to content

Commit e9f12dc

Browse files
committed
✨ [104] recursive way
1 parent 12027d6 commit e9f12dc

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

104/my_solution.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@
1111
* @return {number}
1212
*/
1313
const maxDepth = (root) => {
14-
let depth = 0;
15-
16-
const dfs = (currNode, tmpDepth) => {
17-
if (null !== currNode) {
18-
tmpDepth++;
19-
dfs(currNode.left, tmpDepth);
20-
dfs(currNode.right, tmpDepth);
21-
} else {
22-
depth = Math.max(depth, tmpDepth);
23-
return;
24-
}
25-
}
26-
27-
dfs(root, 0);
28-
29-
return depth;
14+
if (null === root) return 0;
15+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
3016
};
17+
18+
// const maxDepth = (root) => {
19+
// let depth = 0;
20+
//
21+
// const dfs = (currNode, tmpDepth) => {
22+
// if (null !== currNode) {
23+
// tmpDepth++;
24+
// dfs(currNode.left, tmpDepth);
25+
// dfs(currNode.right, tmpDepth);
26+
// } else {
27+
// depth = Math.max(depth, tmpDepth);
28+
// return;
29+
// }
30+
// }
31+
//
32+
// dfs(root, 0);
33+
//
34+
// return depth;
35+
// };

0 commit comments

Comments
 (0)