File tree 1 file changed +21
-16
lines changed
1 file changed +21
-16
lines changed Original file line number Diff line number Diff line change 11
11
* @return {number }
12
12
*/
13
13
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 ;
30
16
} ;
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
+ // };
You can’t perform that action at this time.
0 commit comments