Skip to content

Commit f17aa7d

Browse files
committed
feat: add solution of Convert Sorted Array to Binary Search Tree(108) with javascript.
1 parent ed9e7e4 commit f17aa7d

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@
7676
| [100][100-question] | [Same Tree][100-tips] | [Easy][E] | [][100-java] | [][100-js] | [][100-kotlin] |
7777
| [101][101-question] | [Symmetric Tree][101-tips] | [Easy][E] | [][101-java] | [][101-js] | [][101-kotlin] |
7878
| [104][104-question] | [Maximum Depth of Binary Tree][104-tips] | [Easy][E] | [][104-java] | [][104-js] | [][104-kotlin] |
79-
| [107][107-question] | [Binary Tree Level Order Traversal II][107-tips] | [Easy][E] | [][107-java] | | [][107-kotlin] |
80-
| [108][108-question] | [Convert Sorted Array to Binary Search Tree][108-tips] | [Easy][E] | [][108-java] | | [][108-kotlin] |
79+
| [107][107-question] | [Binary Tree Level Order Traversal II][107-tips] | [Easy][E] | [][107-java] | [][107-js] | [][107-kotlin] |
80+
| [108][108-question] | [Convert Sorted Array to Binary Search Tree][108-tips] | [Easy][E] | [][108-java] | [][108-js] | [][108-kotlin] |
8181
| [110][110-question] | [Balanced Binary Tree][110-tips] | [Easy][E] | [][110-java] | | [][110-kotlin] |
8282
| [111][111-question] | [Minimum Depth of Binary Tree][111-tips] | [Easy][E] | [][111-java] | | [][111-kotlin] |
8383
| [112][112-question] | [Path Sum][112-tips] | [Easy][E] | [][112-java] | | [][112-kotlin] |
@@ -456,6 +456,8 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
456456
[100-js]: ./src/_100/Solution.js
457457
[101-js]: ./src/_101/Solution.js
458458
[104-js]: ./src/_104/Solution.js
459+
[107-js]: ./src/_107/Solution.js
460+
[108-js]: ./src/_108/Solution.js
459461
[226-js]: ./src/_226/Solution.js
460462
[561-js]: ./src/_561/Solution.js
461463
[643-js]: ./src/_643/Solution.js

src/_108/Solution.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {number[]} nums
10+
* @return {TreeNode}
11+
*/
12+
var sortedArrayToBST = function(nums) {
13+
if (nums == null || !nums.length) {
14+
return null;
15+
}
16+
let mid = Math.floor(nums.length / 2)
17+
let rootNode = new TreeNode(nums[mid])
18+
rootNode.left = sortedArrayToBST(nums.slice(0, mid))
19+
rootNode.right = sortedArrayToBST(nums.slice(mid+1))
20+
return rootNode
21+
};

tips/101/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ class Solution {
7878
}
7979
```
8080

81+
82+
```javascript
83+
var isSymmetric = function(root) {
84+
if(root == null) return true
85+
var x = function(left, right) {
86+
if(left == null && right == null) return true
87+
if(left == null || right == null) return false
88+
if (left.val !== right.val) return false;
89+
return x(left.left, right.right) && x(left.right, right.left)
90+
}
91+
return x(root.left, root.right)
92+
};
93+
```
8194
## 思路 1
8295

8396
第二种思路就是宽搜了,宽搜肯定要用到队列,Java 中可用 `LinkedList` 替代,也是要做到左子树的左节点和右子树的右节点,左子树的右节点和右子树的左节点做比较即可。

tips/104/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class Solution {
4444
}
4545
```
4646

47+
```javascript
48+
var maxDepth = function(root) {
49+
if (root == null) return 0;
50+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
51+
};
52+
```
4753
## 结语
4854

4955
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]

tips/107/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,25 @@ class Solution {
119119
}
120120
```
121121

122+
```javascript
123+
var levelOrderBottom = function(root) {
124+
if (root === null) { return []; }
125+
let result = [];
126+
let queue = [root];
127+
while(queue.length > 0) {
128+
let size = queue.length;
129+
let current = [];
130+
for (let i = 0; i < size; i++) {
131+
let head = queue.shift();
132+
current.push(head.val);
133+
if(head.left !== null) { queue.push(head.left); }
134+
if(head.right !== null) { queue.push(head.right); }
135+
}
136+
result.unshift(current);
137+
}
138+
return result;
139+
};
140+
```
122141
## 结语
123142

124143
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]

tips/108/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ class Solution {
8585
}
8686
```
8787

88+
```javascript
89+
var sortedArrayToBST = function(nums) {
90+
if (nums == null || !nums.length) {
91+
return null;
92+
}
93+
let mid = Math.floor(nums.length / 2)
94+
let rootNode = new TreeNode(nums[mid])
95+
rootNode.left = sortedArrayToBST(nums.slice(0, mid))
96+
rootNode.right = sortedArrayToBST(nums.slice(mid+1))
97+
return rootNode
98+
};
99+
```
88100
## 结语
89101

90102
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]

0 commit comments

Comments
 (0)