-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten-binary-tree-to-linked-list.cc
65 lines (52 loc) · 1.13 KB
/
flatten-binary-tree-to-linked-list.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
void flatten(TreeNode *root, TreeNode *&prev) {
if (root == NULL) {
return;
}
if (prev != NULL) {
prev->right = root;
}
prev = root;
// 保存right孩子,防止递归左子树时修改了此节点的right指针
TreeNode *right = root->right;
flatten(root->left, prev);
root->left = NULL;
flatten(right, prev);
}
void flatten(TreeNode *root) {
// prev保存线序遍历的前驱
TreeNode *prev = NULL;
flatten(root, prev);
}
};
int main(int argc, char const* argv[])
{
TreeNode *a1 = new TreeNode(1);
TreeNode *a2 = new TreeNode(2);
TreeNode *a3 = new TreeNode(3);
TreeNode *a4 = new TreeNode(4);
TreeNode *a5 = new TreeNode(5);
TreeNode *a6 = new TreeNode(6);
a1->left = a2;
a1->right = a5;
a2->left = a3;
a2->right = a4;
a5->right = a6;
Solution s;
s.flatten(a1);
while (a1) {
cout << a1->val << " ";
a1 = a1->right;
}
return 0;
}