Skip to content

Commit 673cd92

Browse files
committed
Day61
1 parent 8c46d9c commit 673cd92

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Day61/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 {TreeNode}
12+
*/
13+
var subtreeWithAllDeepest = function (root) {
14+
15+
let result;
16+
17+
function helper(node) {
18+
if (!node) return;
19+
20+
let left = height(node.left);
21+
let right = height(node.right);
22+
23+
if (left == right) {
24+
result = node;
25+
return
26+
} else {
27+
if (left > right) {
28+
helper(node.left)
29+
} else {
30+
helper(node.right)
31+
}
32+
}
33+
}
34+
35+
helper(root)
36+
return result;
37+
38+
39+
function height(node) {
40+
if (!node) return 0;
41+
let left = height(node.left);
42+
let right = height(node.right);
43+
return Math.max(left, right) + 1;
44+
}
45+
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,6 @@ If you are loving solving problems in leetcode, please contact me to enjoy it to
137137

138138
|Day 59| [1. Two Sum](https://leetcode.com/problems/two-sum/) | [javascript]()|[:memo:](https://leetcode.com/problems/two-sum/)|Easy|
139139

140-
|Day 60| [80. Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [javascript]()|[:memo:](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)|Medium|
140+
|Day 60| [80. Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [javascript]()|[:memo:](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)|Medium|
141+
142+
|Day 61| [865. Smallest Subtree with all the Deepest Nodes](https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/) | [javascript]()|[:memo:](https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/)|Medium|

0 commit comments

Comments
 (0)