diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md
index 93642de5..64e9de3f 100644
--- a/problems/0235.二叉搜索树的最近公共祖先.md
+++ b/problems/0235.二叉搜索树的最近公共祖先.md
@@ -247,8 +247,23 @@ class Solution {
```
Python:
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, x):
+# self.val = x
+# self.left = None
+# self.right = None
-
+class Solution:
+ def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
+ if not root: return root //中
+ if root.val >p.val and root.val > q.val:
+ return self.lowestCommonAncestor(root.left,p,q) //左
+ elif root.val < p.val and root.val < q.val:
+ return self.lowestCommonAncestor(root.right,p,q) //右
+ else: return root
+```
Go:
@@ -258,4 +273,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md
index 3233c6a1..a32c017e 100644
--- a/problems/0236.二叉树的最近公共祖先.md
+++ b/problems/0236.二叉树的最近公共祖先.md
@@ -263,8 +263,24 @@ class Solution {
```
Python:
-
-
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, x):
+# self.val = x
+# self.left = None
+# self.right = None
+//递归
+class Solution:
+ def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
+ if not root or root == p or root == q: return root //找到了节点p或者q,或者遇到空节点
+ left = self.lowestCommonAncestor(root.left,p,q) //左
+ right = self.lowestCommonAncestor(root.right,p,q) //右
+ if left and right: return root //中: left和right不为空,root就是最近公共节点
+ elif left and not right: return left //目标节点是通过left返回的
+ elif not left and right: return right //目标节点是通过right返回的
+ else: return None //没找到
+```
Go:
```Go
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md
index 756ecc86..0260a34b 100644
--- a/problems/0332.重新安排行程.md
+++ b/problems/0332.重新安排行程.md
@@ -399,6 +399,49 @@ char ** findItinerary(char *** tickets, int ticketsSize, int* ticketsColSize, in
}
```
+Javascript:
+```Javascript
+
+var findItinerary = function(tickets) {
+ let result = ['JFK']
+ let map = {}
+
+ for (const tickt of tickets) {
+ const [from, to] = tickt
+ if (!map[from]) {
+ map[from] = []
+ }
+ map[from].push(to)
+ }
+
+ for (const city in map) {
+ // 对到达城市列表排序
+ map[city].sort()
+ }
+ function backtracing() {
+ if (result.length === tickets.length + 1) {
+ return true
+ }
+ if (!map[result[result.length - 1]] || !map[result[result.length - 1]].length) {
+ return false
+ }
+ for(let i = 0 ; i < map[result[result.length - 1]].length; i++) {
+ let city = map[result[result.length - 1]][i]
+ // 删除已走过航线,防止死循环
+ map[result[result.length - 1]].splice(i, 1)
+ result.push(city)
+ if (backtracing()) {
+ return true
+ }
+ result.pop()
+ map[result[result.length - 1]].splice(i, 0, city)
+ }
+ }
+ backtracing()
+ return result
+};
+
+```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
diff --git a/problems/0435.无重叠区间.md b/problems/0435.无重叠区间.md
index 4341f3b8..540a82e3 100644
--- a/problems/0435.无重叠区间.md
+++ b/problems/0435.无重叠区间.md
@@ -212,7 +212,19 @@ class Solution {
```
Python:
-
+```python
+class Solution:
+ def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
+ if len(intervals) == 0: return 0
+ intervals.sort(key=lambda x: x[1])
+ count = 1 # 记录非交叉区间的个数
+ end = intervals[0][1] # 记录区间分割点
+ for i in range(1, len(intervals)):
+ if end <= intervals[i][0]:
+ count += 1
+ end = intervals[i][1]
+ return len(intervals) - count
+```
Go:
@@ -223,4 +235,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md
index 604fb376..2cac8f87 100644
--- a/problems/0450.删除二叉搜索树中的节点.md
+++ b/problems/0450.删除二叉搜索树中的节点.md
@@ -281,7 +281,43 @@ class Solution {
```
Python:
-
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, val=0, left=None, right=None):
+# self.val = val
+# self.left = left
+# self.right = right
+class Solution:
+ def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
+ if not root: return root #第一种情况:没找到删除的节点,遍历到空节点直接返回了
+ if root.val == key:
+ if not root.left and not root.right: #第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
+ del root
+ return None
+ if not root.left and root.right: #第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
+ tmp = root
+ root = root.right
+ del tmp
+ return root
+ if root.left and not root.right: #第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
+ tmp = root
+ root = root.left
+ del tmp
+ return root
+ else: #第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
+ v = root.right
+ while v.left:
+ v = v.left
+ v.left = root.left
+ tmp = root
+ root = root.right
+ del tmp
+ return root
+ if root.val > key: root.left = self.deleteNode(root.left,key) #左递归
+ if root.val < key: root.right = self.deleteNode(root.right,key) #右递归
+ return root
+```
Go:
```Go
@@ -330,4 +366,4 @@ func deleteNode1(root *TreeNode)*TreeNode{
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md
index f62fb153..45c94a01 100644
--- a/problems/0452.用最少数量的箭引爆气球.md
+++ b/problems/0452.用最少数量的箭引爆气球.md
@@ -70,7 +70,7 @@
其实都可以!只不过对应的遍历顺序不同,我就按照气球的起始位置排序了。
-既然按照其实位置排序,那么就从前向后遍历气球数组,靠左尽可能让气球重复。
+既然按照起始位置排序,那么就从前向后遍历气球数组,靠左尽可能让气球重复。
从前向后遍历遇到重叠的气球了怎么办?
@@ -167,7 +167,19 @@ class Solution {
```
Python:
-
+```python
+class Solution:
+ def findMinArrowShots(self, points: List[List[int]]) -> int:
+ if len(points) == 0: return 0
+ points.sort(key=lambda x: x[0])
+ result = 1
+ for i in range(1, len(points)):
+ if points[i][0] > points[i - 1][1]: # 气球i和气球i-1不挨着,注意这里不是>=
+ result += 1
+ else:
+ points[i][1] = min(points[i - 1][1], points[i][1]) # 更新重叠气球最小右边界
+ return result
+```
Go:
@@ -178,4 +190,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md
index 1782c88c..65d9b4e2 100644
--- a/problems/0494.目标和.md
+++ b/problems/0494.目标和.md
@@ -225,7 +225,7 @@ public:
是的,如果仅仅是求个数的话,就可以用dp,但[回溯算法:39. 组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)要求的是把所有组合列出来,还是要使用回溯法爆搜的。
-本地还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
+本题还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
```
dp[j] += dp[j - nums[i]];
@@ -272,4 +272,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md
index 385ce2f1..0ddd8b0c 100644
--- a/problems/0501.二叉搜索树中的众数.md
+++ b/problems/0501.二叉搜索树中的众数.md
@@ -394,8 +394,39 @@ class Solution {
```
Python:
-
-
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, val=0, left=None, right=None):
+# self.val = val
+# self.left = left
+# self.right = right
+//递归法
+class Solution:
+ def findMode(self, root: TreeNode) -> List[int]:
+ if not root: return
+ self.pre = root
+ self.count = 0 //统计频率
+ self.countMax = 0 //最大频率
+ self.res = []
+ def findNumber(root):
+ if not root: return None // 第一个节点
+ findNumber(root.left) //左
+ if self.pre.val == root.val: //中: 与前一个节点数值相同
+ self.count += 1
+ else: // 与前一个节点数值不同
+ self.pre = root
+ self.count = 1
+ if self.count > self.countMax: // 如果计数大于最大值频率
+ self.countMax = self.count // 更新最大频率
+ self.res = [root.val] //更新res
+ elif self.count == self.countMax: // 如果和最大值相同,放进res中
+ self.res.append(root.val)
+ findNumber(root.right) //右
+ return
+ findNumber(root)
+ return self.res
+```
Go:
@@ -405,4 +436,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md
index 112c96f9..5afcd6b8 100644
--- a/problems/0509.斐波那契数.md
+++ b/problems/0509.斐波那契数.md
@@ -207,6 +207,19 @@ class Solution:
```
Go:
+```Go
+func fib(n int) int {
+ if n < 2 {
+ return n
+ }
+ a, b, c := 0, 1, 0
+ for i := 1; i < n; i++ {
+ c = a + b
+ a, b = b, c
+ }
+ return c
+}
+```
diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md
index b8158205..aff56208 100644
--- a/problems/0746.使用最小花费爬楼梯.md
+++ b/problems/0746.使用最小花费爬楼梯.md
@@ -228,7 +228,23 @@ Python:
Go:
+```Go
+func minCostClimbingStairs(cost []int) int {
+ dp := make([]int, len(cost))
+ dp[0], dp[1] = cost[0], cost[1]
+ for i := 2; i < len(cost); i++ {
+ dp[i] = min(dp[i-1], dp[i-2]) + cost[i]
+ }
+ return min(dp[len(cost)-1], dp[len(cost)-2])
+}
+func min(a, b int) int {
+ if a < b {
+ return a
+ }
+ return b
+}
+```
diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md
index 607a4ccf..32a29c2a 100644
--- a/problems/1047.删除字符串中的所有相邻重复项.md
+++ b/problems/1047.删除字符串中的所有相邻重复项.md
@@ -122,6 +122,8 @@ public:
Java:
+
+使用 Deque 作为堆栈
```Java
class Solution {
public String removeDuplicates(String S) {
@@ -144,6 +146,30 @@ class Solution {
}
}
```
+拿字符串直接作为栈,省去了栈还要转为字符串的操作。
+```Java
+class Solution {
+ public String removeDuplicates(String s) {
+ // 将 res 当做栈
+ StringBuffer res = new StringBuffer();
+ // top为 res 的长度
+ int top = -1;
+ for (int i = 0; i < s.length(); i++) {
+ char c = s.charAt(i);
+ // 当 top > 0,即栈中有字符时,当前字符如果和栈中字符相等,弹出栈顶字符,同时 top--
+ if (top >= 0 && res.charAt(top) == c) {
+ res.deleteCharAt(top);
+ top--;
+ // 否则,将该字符 入栈,同时top++
+ } else {
+ res.append(c);
+ top++;
+ }
+ }
+ return res.toString();
+ }
+}
+```
Python:
```python3
diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md
index 2d571294..e5279ec0 100644
--- a/problems/二叉树的递归遍历.md
+++ b/problems/二叉树的递归遍历.md
@@ -306,6 +306,59 @@ var postorderTraversal = function(root, res = []) {
return res;
};
```
+Javascript版本:
+
+前序遍历:
+```Javascript
+var preorderTraversal = function(root) {
+ let res=[];
+ const dfs=function(root){
+ if(root===null)return ;
+ //先序遍历所以从父节点开始
+ res.push(root.val);
+ //递归左子树
+ dfs(root.left);
+ //递归右子树
+ dfs(root.right);
+ }
+ //只使用一个参数 使用闭包进行存储结果
+ dfs(root);
+ return res;
+};
+```
+中序遍历
+```javascript
+var inorderTraversal = function(root) {
+ let res=[];
+ const dfs=function(root){
+ if(root===null){
+ return ;
+ }
+ dfs(root.left);
+ res.push(root.val);
+ dfs(root.right);
+ }
+ dfs(root);
+ return res;
+};
+```
+
+后序遍历
+```javascript
+var postorderTraversal = function(root) {
+ let res=[];
+ const dfs=function(root){
+ if(root===null){
+ return ;
+ }
+ dfs(root.left);
+ dfs(root.right);
+ res.push(root.val);
+ }
+ dfs(root);
+ return res;
+};
+```