Skip to content

Commit 52b15f3

Browse files
committed
feat: update solutions to lc problems: No.0094,2034
* No.0094.Binary Tree Inorder Traversal * No.2034.Stock Price Fluctuation
1 parent 0aff2a4 commit 52b15f3

File tree

12 files changed

+437
-381
lines changed

12 files changed

+437
-381
lines changed

solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md

+142-97
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
非递归的思路如下:
7272

73-
1. 定义一个栈
73+
1. 定义一个栈 stk
7474
2. 将树的左节点依次入栈
7575
3. 左节点为空时,弹出栈顶元素并处理
7676
4. 重复 2-3 的操作
@@ -81,10 +81,10 @@ Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
8181

8282
遍历二叉树节点,
8383

84-
1. 若当前节点 root 的左子树为空,**将当前节点值添加至结果列表 res** 中,并将当前节点更新为 `root.right`
85-
2. 若当前节点 root 的左子树不为空,找到左子树的最右节点 pre(也即是 root 节点在中序遍历下的前驱节点):
86-
- 若前驱节点 pre 的右子树为空,将前驱节点的右子树指向当前节点 root,并将当前节点更新为 `root.left`
87-
- 若前驱节点 pre 的右子树不为空,**将当前节点值添加至结果列表 res** 中,然后将前驱节点右子树指向空(即解除 pre 与 root 的指向关系),并将当前节点更新为 `root.right`
84+
1. 若当前节点 root 的左子树为空,**将当前节点值添加至结果列表 ans** 中,并将当前节点更新为 `root.right`
85+
2. 若当前节点 root 的左子树不为空,找到左子树的最右节点 prev(也即是 root 节点在中序遍历下的前驱节点):
86+
- 若前驱节点 prev 的右子树为空,将前驱节点的右子树指向当前节点 root,并将当前节点更新为 `root.left`
87+
- 若前驱节点 prev 的右子树不为空,**将当前节点值添加至结果列表 ans** 中,然后将前驱节点右子树指向空(即解除 prev 与 root 的指向关系),并将当前节点更新为 `root.right`
8888
3. 循环以上步骤,直至二叉树节点为空,遍历结束。
8989

9090
<!-- tabs:start -->
@@ -103,17 +103,18 @@ Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
103103
# self.left = left
104104
# self.right = right
105105
class Solution:
106-
def inorderTraversal(self, root: TreeNode) -> List[int]:
107-
res = []
108-
109-
def inorder(root):
110-
if root:
111-
inorder(root.left)
112-
res.append(root.val)
113-
inorder(root.right)
114-
115-
inorder(root)
116-
return res
106+
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
107+
def dfs(root):
108+
if root is None:
109+
return
110+
dfs(root.left)
111+
nonlocal ans
112+
ans.append(root.val)
113+
dfs(root.right)
114+
115+
ans = []
116+
dfs(root)
117+
return ans
117118
```
118119

119120
栈实现非递归:
@@ -126,17 +127,17 @@ class Solution:
126127
# self.left = left
127128
# self.right = right
128129
class Solution:
129-
def inorderTraversal(self, root: TreeNode) -> List[int]:
130-
res, s = [], []
131-
while root or s:
130+
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
131+
ans, stk = [], []
132+
while root or stk:
132133
if root:
133-
s.append(root)
134+
stk.append(root)
134135
root = root.left
135136
else:
136-
root = s.pop()
137-
res.append(root.val)
137+
root = stk.pop()
138+
ans.append(root.val)
138139
root = root.right
139-
return res
140+
return ans
140141
```
141142

142143
Morris 遍历:
@@ -149,24 +150,24 @@ Morris 遍历:
149150
# self.left = left
150151
# self.right = right
151152
class Solution:
152-
def inorderTraversal(self, root: TreeNode) -> List[int]:
153-
res = []
153+
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
154+
ans = []
154155
while root:
155156
if root.left is None:
156-
res.append(root.val)
157+
ans.append(root.val)
157158
root = root.right
158159
else:
159-
pre = root.left
160-
while pre.right and pre.right != root:
161-
pre = pre.right
162-
if pre.right is None:
163-
pre.right = root
160+
prev = root.left
161+
while prev.right and prev.right != root:
162+
prev = prev.right
163+
if prev.right is None:
164+
prev.right = root
164165
root = root.left
165166
else:
166-
res.append(root.val)
167-
pre.right = None
167+
ans.append(root.val)
168+
prev.right = None
168169
root = root.right
169-
return res
170+
return ans
170171
```
171172

172173
### **Java**
@@ -192,18 +193,21 @@ class Solution:
192193
* }
193194
*/
194195
class Solution {
196+
private List<Integer> ans;
197+
195198
public List<Integer> inorderTraversal(TreeNode root) {
196-
List<Integer> res = new ArrayList<>();
197-
inorder(root, res);
198-
return res;
199+
ans = new ArrayList<>();
200+
dfs(root);
201+
return ans;
199202
}
200203

201-
private void inorder(TreeNode root, List<Integer> res) {
202-
if (root != null) {
203-
inorder(root.left, res);
204-
res.add(root.val);
205-
inorder(root.right, res);
204+
private void dfs(TreeNode root) {
205+
if (root == null) {
206+
return;
206207
}
208+
dfs(root.left);
209+
ans.add(root.val);
210+
dfs(root.right);
207211
}
208212
}
209213
```
@@ -228,19 +232,19 @@ class Solution {
228232
*/
229233
class Solution {
230234
public List<Integer> inorderTraversal(TreeNode root) {
231-
List<Integer> res = new ArrayList<>();
232-
Deque<TreeNode> s = new LinkedList<>();
233-
while (root != null || !s.isEmpty()) {
235+
List<Integer> ans = new ArrayList<>();
236+
Deque<TreeNode> stk = new ArrayDeque<>();
237+
while (root != null || !stk.isEmpty()) {
234238
if (root != null) {
235-
s.offerLast(root);
239+
stk.push(root);
236240
root = root.left;
237241
} else {
238-
root = s.pollLast();
239-
res.add(root.val);
242+
root = stk.pop();
243+
ans.add(root.val);
240244
root = root.right;
241245
}
242246
}
243-
return res;
247+
return ans;
244248
}
245249
}
246250
```
@@ -265,27 +269,27 @@ Morris 遍历:
265269
*/
266270
class Solution {
267271
public List<Integer> inorderTraversal(TreeNode root) {
268-
List<Integer> res = new ArrayList<>();
272+
List<Integer> ans = new ArrayList<>();
269273
while (root != null) {
270274
if (root.left == null) {
271-
res.add(root.val);
275+
ans.add(root.val);
272276
root = root.right;
273277
} else {
274-
TreeNode pre = root.left;
275-
while (pre.right != null && pre.right != root) {
276-
pre = pre.right;
278+
TreeNode prev = root.left;
279+
while (prev.right != null && prev.right != root) {
280+
prev = prev.right;
277281
}
278-
if (pre.right == null) {
279-
pre.right = root;
282+
if (prev.right == null) {
283+
prev.right = root;
280284
root = root.left;
281285
} else {
282-
res.add(root.val);
283-
pre.right = null;
286+
ans.add(root.val);
287+
prev.right = null;
284288
root = root.right;
285289
}
286290
}
287291
}
288-
return res;
292+
return ans;
289293
}
290294
}
291295
```
@@ -308,16 +312,15 @@ class Solution {
308312
* @return {number[]}
309313
*/
310314
var inorderTraversal = function (root) {
311-
let res = [];
312-
function inorder(root) {
313-
if (root) {
314-
inorder(root.left);
315-
res.push(root.val);
316-
inorder(root.right);
317-
}
315+
let ans = [];
316+
function dfs(root) {
317+
if (!root) return;
318+
dfs(root.left);
319+
ans.push(root.val);
320+
dfs(root.right);
318321
}
319-
inorder(root);
320-
return res;
322+
dfs(root);
323+
return ans;
321324
};
322325
```
323326

@@ -337,19 +340,59 @@ var inorderTraversal = function (root) {
337340
* @return {number[]}
338341
*/
339342
var inorderTraversal = function (root) {
340-
let res = [];
341-
let s = [];
342-
while (root || s.length > 0) {
343+
let ans = [],
344+
stk = [];
345+
while (root || stk.length > 0) {
343346
if (root) {
344-
s.push(root);
347+
stk.push(root);
345348
root = root.left;
346349
} else {
347-
root = s.pop();
348-
res.push(root.val);
350+
root = stk.pop();
351+
ans.push(root.val);
349352
root = root.right;
350353
}
351354
}
352-
return res;
355+
return ans;
356+
};
357+
```
358+
359+
Morris 遍历:
360+
361+
```js
362+
/**
363+
* Definition for a binary tree node.
364+
* function TreeNode(val, left, right) {
365+
* this.val = (val===undefined ? 0 : val)
366+
* this.left = (left===undefined ? null : left)
367+
* this.right = (right===undefined ? null : right)
368+
* }
369+
*/
370+
/**
371+
* @param {TreeNode} root
372+
* @return {number[]}
373+
*/
374+
var inorderTraversal = function (root) {
375+
let ans = [];
376+
while (root) {
377+
if (!root.left) {
378+
ans.push(root.val);
379+
root = root.right;
380+
} else {
381+
let prev = root.left;
382+
while (prev.right && prev.right != root) {
383+
prev = prev.right;
384+
}
385+
if (!prev.right) {
386+
prev.right = root;
387+
root = root.left;
388+
} else {
389+
ans.push(root.val);
390+
prev.right = null;
391+
root = root.right;
392+
}
393+
}
394+
}
395+
return ans;
353396
};
354397
```
355398

@@ -370,33 +413,35 @@ var inorderTraversal = function (root) {
370413
class Solution {
371414
public:
372415
vector<int> inorderTraversal(TreeNode* root) {
373-
vector<int> res;
416+
vector<int> ans;
374417
while (root)
375418
{
376-
if (root->left == nullptr)
419+
if (!root->left)
377420
{
378-
res.push_back(root->val);
421+
ans.push_back(root->val);
379422
root = root->right;
380-
} else {
381-
TreeNode* pre = root->left;
382-
while (pre->right && pre->right != root)
423+
}
424+
else
425+
{
426+
TreeNode* prev = root->left;
427+
while (prev->right && prev->right != root)
383428
{
384-
pre = pre->right;
429+
prev = prev->right;
385430
}
386-
if (pre->right == nullptr)
431+
if (!prev->right)
387432
{
388-
pre->right = root;
433+
prev->right = root;
389434
root = root->left;
390435
}
391436
else
392437
{
393-
res.push_back(root->val);
394-
pre->right = nullptr;
438+
ans.push_back(root->val);
439+
prev->right = nullptr;
395440
root = root->right;
396441
}
397442
}
398443
}
399-
return res;
444+
return ans;
400445
}
401446
};
402447
```
@@ -413,27 +458,27 @@ public:
413458
* }
414459
*/
415460
func inorderTraversal(root *TreeNode) []int {
416-
var res []int
461+
var ans []int
417462
for root != nil {
418463
if root.Left == nil {
419-
res = append(res, root.Val)
464+
ans = append(ans, root.Val)
420465
root = root.Right
421466
} else {
422-
pre := root.Left
423-
for pre.Right != nil && pre.Right != root {
424-
pre = pre.Right
467+
prev := root.Left
468+
for prev.Right != nil && prev.Right != root {
469+
prev = prev.Right
425470
}
426-
if pre.Right == nil {
427-
pre.Right = root
471+
if prev.Right == nil {
472+
prev.Right = root
428473
root = root.Left
429474
} else {
430-
res = append(res, root.Val)
431-
pre.Right = nil
475+
ans = append(ans, root.Val)
476+
prev.Right = nil
432477
root = root.Right
433478
}
434479
}
435480
}
436-
return res
481+
return ans
437482
}
438483
```
439484

0 commit comments

Comments
 (0)