Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d9d102

Browse files
author
luzhipeng
committed
前序, 后序遍历, 找出单个数字
1 parent cca8a3f commit 4d9d102

File tree

3 files changed

+374
-0
lines changed

3 files changed

+374
-0
lines changed

136.single-number.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
2+
## 题目地址
3+
https://leetcode.com/problems/single-number/description/
4+
5+
## 题目描述
6+
7+
```
8+
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
9+
10+
Note:
11+
12+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
13+
```
14+
## 思路
15+
16+
根据题目描述,由于加上了时间复杂度必须是O(n),并且空间复杂度为O(1)的条件,因此不能用排序方法,也不能使用map数据结构。
17+
18+
我们可以利用二进制异或的性质来完成,将所有数字异或即得到唯一出现的数字。
19+
20+
## 关键点
21+
1. 异或的性质
22+
两个数字异或的结果`a^b`是将a和b的二进制每一位进行运算,得出的数字。 运算的逻辑是
23+
如果同一位的数字相同则为0,不同则为1
24+
25+
2. 异或的规律
26+
27+
- 任何数和本身异或则为`0`
28+
29+
- 任何数和0异或是`本身`
30+
31+
3. 很多人只是记得异或的性质和规律,但是缺乏对其本质的理解,导致很难想到这种解法(我本人也没想到)
32+
33+
## 代码
34+
35+
```js
36+
/*
37+
* @lc app=leetcode id=136 lang=javascript
38+
*
39+
* [136] Single Number
40+
*
41+
* https://leetcode.com/problems/single-number/description/
42+
*
43+
* algorithms
44+
* Easy (59.13%)
45+
* Total Accepted: 429.3K
46+
* Total Submissions: 724.1K
47+
* Testcase Example: '[2,2,1]'
48+
*
49+
* Given a non-empty array of integers, every element appears twice except for
50+
* one. Find that single one.
51+
*
52+
* Note:
53+
*
54+
* Your algorithm should have a linear runtime complexity. Could you implement
55+
* it without using extra memory?
56+
*
57+
* Example 1:
58+
*
59+
*
60+
* Input: [2,2,1]
61+
* Output: 1
62+
*
63+
*
64+
* Example 2:
65+
*
66+
*
67+
* Input: [4,1,2,1,2]
68+
* Output: 4
69+
*
70+
*
71+
*/
72+
/**
73+
* @param {number[]} nums
74+
* @return {number}
75+
*/
76+
var singleNumber = function(nums) {
77+
let ret = 0;
78+
for (let index = 0; index < nums.length; index++) {
79+
const element = nums[index];
80+
ret = ret ^ element;
81+
82+
}
83+
return ret;
84+
};
85+
86+
```
87+
88+
## 延伸
89+
90+
有一个 n 个元素的数组,除了两个数只出现一次外,其余元素都出现两次,让你找出这两个只出现一次的数分别是几,要求时间复杂度为 O(n) 且再开辟的内存空间固定(与 n 无关)。
91+
92+
93+
和上面一样,只是这次不是一个数字,而是两个数字。还是按照上面的思路,我们进行一次全员异或操作,
94+
得到的结果就是那两个只出现一次的不同的数字的异或结果。
95+
96+
我们刚才讲了异或的规律中有一个`任何数和本身异或则为0`, 因此我们的思路是能不能将这两个不同的数字分成两组A和B。
97+
分组需要满足两个条件.
98+
99+
1. 两个独特的的数字分成不同组
100+
101+
2. 相同的数字分成相同组
102+
103+
这样每一组的数据进行异或即可得到那两个数字。
104+
105+
问题的关键点是我们怎么进行分组呢?
106+
107+
由于异或的性质是,同一位相同则为0,不同则为1. 我们将所有数字异或的结果一定不是0,也就是说至少有一位是1.
108+
109+
我们随便取一个, 分组的依据就来了, 就是你取的那一位是0分成1组,那一位是1的分成一组。
110+
这样肯定能保证`2. 相同的数字分成相同组`, 不同的数字会被分成不同组么。 很明显当然可以, 因此我们选择是1,也就是
111+
`两个独特的的数字`在那一位一定是不同的,因此两个独特元素一定会被分成不同组。
112+
113+
Done!
114+
115+

144.binary-tree-preorder-traversal.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
## 题目地址
2+
https://leetcode.com/problems/binary-tree-preorder-traversal/description/
3+
4+
## 题目描述
5+
6+
```
7+
Given a binary tree, return the preorder traversal of its nodes' values.
8+
9+
Example:
10+
11+
Input: [1,null,2,3]
12+
1
13+
\
14+
2
15+
/
16+
3
17+
18+
Output: [1,2,3]
19+
Follow up: Recursive solution is trivial, could you do it iteratively?
20+
21+
```
22+
23+
## 思路
24+
25+
这道题目是前序遍历,这个和之前的`leetcode 94 号问题 - 中序遍历`完全不一回事。
26+
27+
前序遍历是`根左右`的顺序,注意是``开始,那么就很简单。直接先将根节点入栈,然后
28+
看有没有右节点,有则入栈,再看有没有左节点,有则入栈。 然后出栈一个元素,重复即可。
29+
30+
> 其他树的非递归遍历课没这么简单
31+
32+
## 关键点解析
33+
34+
- 二叉树的基本操作(遍历)
35+
> 不同的遍历算法差异还是蛮大的
36+
- 如果非递归的话利用栈来简化操作
37+
38+
- 如果数据规模不大的话,建议使用递归
39+
40+
- 递归的问题需要注意两点,一个是终止条件,一个如何缩小规模
41+
42+
1. 终止条件,自然是当前这个元素是null(链表也是一样)
43+
44+
2. 由于二叉树本身就是一个递归结构, 每次处理一个子树其实就是缩小了规模,
45+
难点在于如何合并结果,这里的合并结果其实就是`mid.concat(left).concat(right)`,
46+
mid是一个具体的节点,left和right`递归求出即可`
47+
48+
49+
## 代码
50+
51+
```js
52+
/*
53+
* @lc app=leetcode id=144 lang=javascript
54+
*
55+
* [144] Binary Tree Preorder Traversal
56+
*
57+
* https://leetcode.com/problems/binary-tree-preorder-traversal/description/
58+
*
59+
* algorithms
60+
* Medium (50.36%)
61+
* Total Accepted: 314K
62+
* Total Submissions: 621.2K
63+
* Testcase Example: '[1,null,2,3]'
64+
*
65+
* Given a binary tree, return the preorder traversal of its nodes' values.
66+
*
67+
* Example:
68+
*
69+
*
70+
* Input: [1,null,2,3]
71+
* ⁠ 1
72+
* ⁠ \
73+
* ⁠ 2
74+
* ⁠ /
75+
* ⁠ 3
76+
*
77+
* Output: [1,2,3]
78+
*
79+
*
80+
* Follow up: Recursive solution is trivial, could you do it iteratively?
81+
*
82+
*/
83+
/**
84+
* Definition for a binary tree node.
85+
* function TreeNode(val) {
86+
* this.val = val;
87+
* this.left = this.right = null;
88+
* }
89+
*/
90+
/**
91+
* @param {TreeNode} root
92+
* @return {number[]}
93+
*/
94+
var preorderTraversal = function(root) {
95+
// 1. Recursive solution
96+
97+
// if (!root) return [];
98+
99+
// return [root.val].concat(preorderTraversal(root.left)).concat(preorderTraversal(root.right));
100+
101+
// 2. iterative solutuon
102+
103+
if (!root) return [];
104+
const ret = [];
105+
const stack = [root];
106+
let left = root.left;
107+
let t = stack.pop();
108+
109+
while(t) {
110+
ret.push(t.val);
111+
if (t.right) {
112+
stack.push(t.right);
113+
}
114+
if (t.left) {
115+
stack.push(t.left);
116+
}
117+
t =stack.pop();
118+
}
119+
120+
return ret;
121+
};
122+
123+
```
124+
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
## 题目地址
2+
https://leetcode.com/problems/binary-tree-postorder-traversal/description/
3+
4+
## 题目描述
5+
6+
```
7+
Given a binary tree, return the postorder traversal of its nodes' values.
8+
9+
For example:
10+
Given binary tree {1,#,2,3},
11+
12+
1
13+
\
14+
2
15+
/
16+
3
17+
18+
19+
return [3,2,1].
20+
21+
Note: Recursive solution is trivial, could you do it iteratively?
22+
23+
```
24+
25+
## 思路
26+
27+
相比于前序遍历,后续遍历思维上难度要大些,前序遍历是通过一个stack,首先压入父亲结点,然后弹出父亲结点,并输出它的value,之后压人其右儿子,左儿子即可。
28+
29+
然而后序遍历结点的访问顺序是:左儿子 -> 右儿子 -> 自己。那么一个结点需要两种情况下才能够输出:
30+
第一,它已经是叶子结点;
31+
第二,它不是叶子结点,但是它的儿子已经输出过。
32+
33+
那么基于此我们只需要记录一下当前输出的结点即可。对于一个新的结点,如果它不是叶子结点,儿子也没有访问,那么就需要将它的右儿子,左儿子压入。
34+
如果它满足输出条件,则输出它,并记录下当前输出结点。输出在stack为空时结束。
35+
36+
37+
## 关键点解析
38+
39+
- 二叉树的基本操作(遍历)
40+
> 不同的遍历算法差异还是蛮大的
41+
- 如果非递归的话利用栈来简化操作
42+
43+
- 如果数据规模不大的话,建议使用递归
44+
45+
- 递归的问题需要注意两点,一个是终止条件,一个如何缩小规模
46+
47+
1. 终止条件,自然是当前这个元素是null(链表也是一样)
48+
49+
2. 由于二叉树本身就是一个递归结构, 每次处理一个子树其实就是缩小了规模,
50+
难点在于如何合并结果,这里的合并结果其实就是`left.concat(right).concat(mid)`,
51+
mid是一个具体的节点,left和right`递归求出即可`
52+
53+
54+
## 代码
55+
56+
```js
57+
/*
58+
* @lc app=leetcode id=145 lang=javascript
59+
*
60+
* [145] Binary Tree Postorder Traversal
61+
*
62+
* https://leetcode.com/problems/binary-tree-postorder-traversal/description/
63+
*
64+
* algorithms
65+
* Hard (47.06%)
66+
* Total Accepted: 242.6K
67+
* Total Submissions: 512.8K
68+
* Testcase Example: '[1,null,2,3]'
69+
*
70+
* Given a binary tree, return the postorder traversal of its nodes' values.
71+
*
72+
* Example:
73+
*
74+
*
75+
* Input: [1,null,2,3]
76+
* ⁠ 1
77+
* ⁠ \
78+
* ⁠ 2
79+
* ⁠ /
80+
* ⁠ 3
81+
*
82+
* Output: [3,2,1]
83+
*
84+
*
85+
* Follow up: Recursive solution is trivial, could you do it iteratively?
86+
*
87+
*/
88+
/**
89+
* Definition for a binary tree node.
90+
* function TreeNode(val) {
91+
* this.val = val;
92+
* this.left = this.right = null;
93+
* }
94+
*/
95+
/**
96+
* @param {TreeNode} root
97+
* @return {number[]}
98+
*/
99+
var postorderTraversal = function(root) {
100+
// 1. Recursive solution
101+
102+
// if (!root) return [];
103+
104+
// return postorderTraversal(root.left).concat(postorderTraversal(root.right)).concat(root.val);
105+
106+
// 2. iterative solutuon
107+
108+
if (!root) return [];
109+
const ret = [];
110+
const stack = [root];
111+
let p = root; // 标识元素,用来判断节点是否应该出栈
112+
113+
while (stack.length > 0) {
114+
const top = stack[stack.length - 1];
115+
if (
116+
top.left === p ||
117+
top.right === p || // 子节点已经遍历过了
118+
(top.left === null && top.right === null) // 叶子元素
119+
) {
120+
p = stack.pop();
121+
ret.push(p.val);
122+
} else {
123+
if (top.right) {
124+
stack.push(top.right);
125+
}
126+
if (top.left) {
127+
stack.push(top.left);
128+
}
129+
}
130+
}
131+
132+
return ret;
133+
};
134+
135+
```

0 commit comments

Comments
 (0)