Skip to content

Commit 12027d6

Browse files
committed
✨ [104] tree/dfs
1 parent e73e20a commit 12027d6

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

104/my_solution.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
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;
30+
};

104/solution.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
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;
30+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- [76. Minimum Window Substring](./76/)
3737
- [79. Word Search](./79/)
3838
- [83. Remove Duplicates from Sorted List](./83/)
39+
- [104. Maximum Depth of Binary Tree](./104/)
3940
- [121. Best Time to Buy and Sell Stock](./121/)
4041
- [128. Longest Consecutive Sequence](./128/)
4142
- [133. Clone Graph](./133/)
@@ -136,7 +137,7 @@ Batch create:
136137
NOTE: JS IS HERE
137138
-->
138139
```ssh
139-
chapter=714 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
140+
chapter=104 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
140141
```
141142
> then you can use `x` for quick debug.
142143

0 commit comments

Comments
 (0)