-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten_binary_tree_to_linked_list.cpp
107 lines (90 loc) · 2.26 KB
/
flatten_binary_tree_to_linked_list.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/
// Date: Nov 21, 2014
#include <iostream>
#include <stack>
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)
{
//experiment
//root->left = NULL;
//return;
if (root == NULL)
{
return;
}
//TreeNode* newRoot = new TreeNode(root->val);
//TreeNode* prevNode = newRoot;
TreeNode* prevNode = root;
stack<TreeNode*> stk;
if (root->right)
{
stk.push(root->right);
root->right = NULL;
}
if (root->left)
{
stk.push(root->left);
root->left = NULL;
}
while (!stk.empty())
{
TreeNode* topNode = stk.top();
stk.pop();
/*
TreeNode* newNode = new TreeNode(topNode->val);
prevNode->right = newNode;
prevNode = newNode;
*/
if (topNode->right)
{
stk.push(topNode->right);
topNode->right = NULL;
}
if (topNode->left)
{
stk.push(topNode->left);
topNode->left = NULL;
}
prevNode->right = topNode;
prevNode = topNode;
}
//root = &*newRoot;
int gh = 0;
}
};
TreeNode* create_binary_tree()
{
TreeNode* root = new TreeNode(1);
TreeNode* node = new TreeNode(2);
root->left = node;
TreeNode* leftNode = new TreeNode(3);
TreeNode* rightNode = new TreeNode(4);
node->left = leftNode;
node->right = rightNode;
node = new TreeNode(5);
root->right = node;
rightNode = new TreeNode(6);
node->right = rightNode;
return root;
}
int main(int argc, char* argv[])
{
TreeNode* root = create_binary_tree();
Solution sln;
sln.flatten(root);
return 0;
}
/*
NU: understand why "root = newRoot;" doesn't works as we come out of the function.
Check if this helps:
http://stackoverflow.com/questions/416162/assignment-inside-function-that-is-passed-as-pointer
*/