Skip to content

Commit 4142c52

Browse files
committed
Flatten Binary Tree to Linked List
1 parent 9abe8fd commit 4142c52

File tree

5 files changed

+270
-0
lines changed

5 files changed

+270
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
+ [111 Minimum Depth of Binary Tree](algorithms/MinimumDepthofBinaryTree)
3535
+ [112 Path Sum(前序遍历)](algorithms/PathSum)
3636
+ [113 Path Sum II(前序遍历)](algorithms/PathSum2)
37+
+ [114 Flatten Binary Tree to Linked List](algorithms/FlattenBinaryTreetoLinkedList)
3738
+ [136 Single Number(位运算)](algorithms/SingleNumber)
3839
+ [141 Linked List Cycle(快慢指针法)](algorithms/LinkedListCycle)
3940
+ [142 Linked List Cycle II](algorithms/LinkedListCycle2)

Diff for: algorithms/FlattenBinaryTreetoLinkedList/README.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
## Flatten Binary Tree to Linked List
2+
3+
Given a binary tree, flatten it to a linked list in-place.
4+
5+
For example,
6+
7+
Given
8+
```
9+
10+
1
11+
/ \
12+
2 5
13+
/ \ \
14+
3 4 6
15+
```
16+
The flattened tree should look like:
17+
```
18+
1
19+
\
20+
2
21+
\
22+
3
23+
\
24+
4
25+
\
26+
5
27+
\
28+
6
29+
```
30+
Hints:
31+
32+
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
33+
34+
## Solution 1
35+
36+
递归方法,首先flatten左子树,然后flatten右子树,此时左子树和右子树都已经是一个链表了,
37+
```
38+
1
39+
/ \
40+
2 5
41+
\ \
42+
3 6
43+
\
44+
4
45+
```
46+
此时只需要合并左右子树即可,即把右子树接到左子树的最右节点,即把5接到4后面,最后把右子树指向左子树即可
47+
48+
** 处理完后,必须把左子树设为`NULL`,否则出现`RunTime ERROR` **
49+
50+
## Code
51+
```cpp
52+
void flatten(TreeNode *root) {
53+
if (root == nullptr)
54+
return ;
55+
flatten(root->left);
56+
flatten(root->right);
57+
if (root->right == nullptr) {
58+
root->right = root->left;
59+
root->left = nullptr; // 不能少这个,否则RE
60+
return;
61+
}
62+
TreeNode *p = root->left;
63+
if (p) {
64+
while (p->right) {
65+
p = p->right;
66+
}
67+
p->right = root->right;
68+
root->right = root->left;
69+
}
70+
root->left = nullptr; // 不能少,否则RE
71+
}
72+
```
73+
74+
## Solution 2
75+
76+
使用循环,从root开始,找到root的左子树的最右节点,然后把右子树接到最右节点,右子树指向左子树,左子树设为`NULL`,然后处理右子树即可
77+
78+
* `root == NULL`, 返回。否则`left = root->left`
79+
* 若`left != NULL`, 找到left的最右子树p,
80+
```
81+
p->right = root->right;
82+
root->right = root->left;
83+
root->left = NULL;
84+
```
85+
* `root = root->right`, 回到最开始。
86+
87+
## Code
88+
```cpp
89+
void flatten(TreeNode *root) {
90+
while (root) {
91+
if (root->left) {
92+
TreeNode *left = root->left;
93+
while (left->right) left = left->right;
94+
left->right = root->right;
95+
root->right = root->left;
96+
root->left = nullptr;
97+
}
98+
root = root->right;
99+
}
100+
}
101+
```

Diff for: algorithms/FlattenBinaryTreetoLinkedList/solve.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <cstdlib>
4+
using namespace std;
5+
struct TreeNode {
6+
int val;
7+
TreeNode *left;
8+
TreeNode *right;
9+
TreeNode(int x) : val(x), left(nullptr), right(nullptr){}
10+
};
11+
class Solution {
12+
public:
13+
void flatten(TreeNode *root) {
14+
slowFlatten(root);
15+
}
16+
private:
17+
void slowFlatten(TreeNode *root) {
18+
if (root == nullptr)
19+
return ;
20+
flatten(root->left);
21+
flatten(root->right);
22+
if (root->right == nullptr) {
23+
root->right = root->left;
24+
root->left = nullptr; // 不能少这个,否则RE
25+
return;
26+
}
27+
TreeNode *p = root->left;
28+
if (p) {
29+
while (p->right) {
30+
p = p->right;
31+
}
32+
p->right = root->right;
33+
root->right = root->left;
34+
}
35+
root->left = nullptr;
36+
}
37+
void fastFlatten(TreeNode *root) {
38+
while (root) {
39+
if (root->left) {
40+
TreeNode *left = root->left;
41+
while (left->right) left = left->right;
42+
left->right = root->right;
43+
root->right = root->left;
44+
root->left = nullptr;
45+
}
46+
root = root->right;
47+
}
48+
}
49+
};
50+
TreeNode *mk_node(int val)
51+
{
52+
return new TreeNode(val);
53+
}
54+
TreeNode *mk_child(TreeNode *root, TreeNode *left, TreeNode *right)
55+
{
56+
root->left = left;
57+
root->right = right;
58+
return root;
59+
}
60+
TreeNode *mk_child(TreeNode *root, int left, int right)
61+
{
62+
return mk_child(root, new TreeNode(left), new TreeNode(right));
63+
}
64+
TreeNode *mk_child(int root, int left, int right)
65+
{
66+
return mk_child(new TreeNode(root), new TreeNode(left), new TreeNode(right));
67+
}
68+
void print_list(TreeNode *root)
69+
{
70+
TreeNode *p = root;
71+
while (p) {
72+
cout << p->val << " ";
73+
p = p->right;
74+
}
75+
cout << endl;
76+
}
77+
int main(int argc, char **argv)
78+
{
79+
Solution solution;
80+
TreeNode *root = mk_child(mk_node(1), nullptr, mk_node(2));
81+
//mk_child(root->left, 3, 4);
82+
//mk_child(root->right, nullptr, mk_node(6));
83+
solution.flatten(root);
84+
print_list(root);
85+
return 0;
86+
}

Diff for: algorithms/Template/list/list.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
struct ListNode {
5+
int val;
6+
struct ListNode *next;
7+
};
8+
int getLength(struct ListNode *head)
9+
{
10+
int len = 0;
11+
struct ListNode *p = head;
12+
while (p) {
13+
++len;
14+
p = p->next;
15+
}
16+
return len;
17+
}
18+
void print(struct ListNode *ha, struct ListNode *hb)
19+
{
20+
struct ListNode *p = getIntersectionNode(ha, hb);
21+
if (p == NULL)
22+
printf("NULL\n");
23+
else
24+
printf("%d\n", p -> val);
25+
}
26+
struct ListNode * mk_list(struct ListNode **ha, int a[], int n)
27+
{
28+
struct ListNode *p = malloc(sizeof(*p));
29+
p->val = a[0];
30+
p->next = NULL;
31+
*ha = p;
32+
for (int i = 1; i < n; ++i) {
33+
struct ListNode *q = malloc(sizeof(*q));
34+
q->val = a[i];
35+
q->next = NULL;
36+
p->next = q;
37+
p = q;
38+
}
39+
return p;
40+
}
41+
int main(int argc, char **argv)
42+
{
43+
return 0;
44+
}

Diff for: algorithms/Template/tree/tree.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
using namespace std;
3+
struct TreeNode {
4+
int val;
5+
TreeNode *left;
6+
TreeNode *right;
7+
TreeNode(int x) : val(x), left(nullptr), right(nullptr){}
8+
}
9+
class Solution {
10+
public:
11+
void foo() {
12+
13+
}
14+
15+
};
16+
TreeNode *mk_node(int val)
17+
{
18+
return new TreeNode(val);
19+
}
20+
TreeNode *mk_child(TreeNode *root, TreeNode *left, TreeNode *right)
21+
{
22+
root->left = left;
23+
root->right = right;
24+
return root;
25+
}
26+
TreeNode *mk_child(TreeNode *root, int left, int right)
27+
{
28+
return mk_child(root, new TreeNode(left), new TreeNode(right));
29+
}
30+
TreeNode *mk_child(int root, int left, int right)
31+
{
32+
return mk_child(new TreeNode(root), new TreeNode(left), new TreeNode(right));
33+
}
34+
int main(int argc, char **argv)
35+
{
36+
Solution solution;
37+
return 0;
38+
}

0 commit comments

Comments
 (0)