Skip to content

Commit 92ee879

Browse files
committed
Time: 0 ms (100%), Space: 58.6 MB (28.17%) - LeetHub
source:cd7cf773d3c5f729cc067010d03cf27787bd174a
1 parent c495a8e commit 92ee879

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
var maxDepth = function(root) {
14+
15+
return dfs(root,1)
16+
17+
function dfs(node, cnt){
18+
if(!node) return 0;
19+
20+
let left, right;
21+
if(node.left) left = dfs(node.left,cnt+1);
22+
if(node.right) right = dfs(node.right,cnt+1);
23+
if(node.left == null && node.right == null) {
24+
return cnt;
25+
}
26+
return Math.max(left ?? 0,right ?? 0);
27+
}
28+
29+
};

0 commit comments

Comments
 (0)