Skip to content

Commit 7125db6

Browse files
committed
add js solution for leetcode 94 144 145
1 parent 1294dec commit 7125db6

File tree

4 files changed

+116
-2
lines changed

4 files changed

+116
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@
281281
|148|[Sort List](https://leetcode.com/problems/sort-list/)| |Medium|
282282
|147|[Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/)| |Medium|
283283
|146|[LRU Cache](https://leetcode.com/problems/lru-cache/)| |Hard|
284-
|145|[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)| |Hard|
284+
|145|[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)| [js](./algorithms/binaryTreePostorderTraversal/binaryTreePostorderTraversal.js) |Hard|
285285
|144|[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)| [js](./algorithms/binaryTreePreorderTraversal/binaryTreePreorderTraversal.js) |Medium|
286286
|143|[Reorder List](https://leetcode.com/problems/reorder-list/)| |Medium|
287287
|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)| |Medium|
@@ -332,7 +332,7 @@
332332
|97|[Interleaving String](https://leetcode.com/problems/interleaving-string/)| |Hard|
333333
|96|[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)| |Medium|
334334
|95|[Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/)| |Medium|
335-
|94|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)| |Medium|
335+
|94|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)| [js](./algorithms/binaryTreeInorderTraversal/binaryTreeInorderTraversal.js) |Medium|
336336
|93|[Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/)| |Medium|
337337
|92|[Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)| |Medium|
338338
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)| |Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var inorderTraversal = function(root) {
2+
if (!root) return [];
3+
4+
const res = [];
5+
let cur1 = root;
6+
let cur2;
7+
8+
while(cur1) {
9+
cur2 = cur1.left;
10+
//构建连接线
11+
if(cur2) {
12+
while (cur2.right && cur2.right !== cur1) {
13+
cur2 = cur2.right;
14+
}
15+
if (!cur2.right) {
16+
cur2.right = cur1;
17+
cur1 = cur1.left;
18+
continue;
19+
} else {
20+
cur2.right = null;
21+
}
22+
}
23+
res.push(cur1.val);
24+
cur1 = cur1.right;
25+
}
26+
27+
return res;
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var postorderTraversal = function(root) {
2+
if (!root) return [];
3+
4+
const res = [];
5+
6+
// 翻转单链表
7+
const postMorrisReverseList = (root) => {
8+
let cur = root;
9+
let pre;
10+
while (cur) {
11+
let next = cur.right;
12+
cur.right = pre;
13+
pre = cur;
14+
cur = next;
15+
}
16+
return pre;
17+
};
18+
// 打印函数
19+
const postMorrisPrint = (root) => {
20+
const reverseList = postMorrisReverseList(root);
21+
let cur = reverseList;
22+
while (cur) {
23+
res.push(cur.val);
24+
cur = cur.right;
25+
}
26+
postMorrisReverseList(reverseList);
27+
};
28+
29+
let cur1 = root; // 遍历树的指针变量
30+
let cur2; // 当前子树的最右节点
31+
32+
while(cur1) {
33+
cur2 = cur1.left;
34+
if(cur2) {
35+
while (cur2.right && cur2.right !== cur1) {
36+
cur2 = cur2.right;
37+
}
38+
if (!cur2.right) {
39+
cur2.right = cur1;
40+
cur1 = cur1.left;
41+
continue;
42+
} else {
43+
cur2.right = null;
44+
postMorrisPrint(cur1.left);
45+
}
46+
}
47+
cur1 = cur1.right;
48+
}
49+
postMorrisPrint(root);
50+
51+
return res;
52+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var preorderTraversal = function(root) {
2+
if (!root) return;
3+
const res = [];
4+
let cur1 = root; // 当前开始遍历的节点
5+
let cur2; // 记录当前结点的左子树
6+
while(cur1) {
7+
cur2 = cur1.left;
8+
if(cur2) {
9+
// 找到当前左子树的最右侧节点,
10+
// 且这个节点应该在指向根结点之前,否则整个节点又回到了根结点。
11+
while (cur2.right && cur2.right !== cur1) {
12+
cur2 = cur2.right;
13+
}
14+
// 这个时候如果最右侧这个节点的右指针没有指向根结点,
15+
// 创建连接然后往下一个左子树的根结点进行连接操作。
16+
if (!cur2.right) {
17+
cur2.right = cur1;
18+
res.push(cur1.val);
19+
cur1 = cur1.left;
20+
continue;
21+
} else { // 当左子树的最右侧节点有指向根结点,
22+
// 此时说明我们已经回到了根结点并重复了之前的操作,
23+
// 同时在回到根结点的时候我们应该已经处理完 左子树的最右侧节点了,把路断开。
24+
cur2.right = null;
25+
}
26+
} else {
27+
// 说明当前是叶子节点了,直接访问就好
28+
res.push(cur1.val);
29+
}
30+
// 一直往右边走
31+
cur1 = cur1.right;
32+
}
33+
return res;
34+
};

0 commit comments

Comments
 (0)