-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatten_binary_tree_to_ll.cpp
More file actions
98 lines (85 loc) · 2.42 KB
/
Copy pathflatten_binary_tree_to_ll.cpp
File metadata and controls
98 lines (85 loc) · 2.42 KB
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
/*
114. Flatten Binary Tree to Linked List
Medium
Given a binary tree, flatten it to a linked list in-place.
For example, given the following tree:
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
//Method 1
//Store all the nodes in a vector in preorder and assign the vector values to the right of each node in tree
//and assign left as NULL
//in place conversion from a tree to a linked list
class Solution {
public:
void preorder(TreeNode* root, vector<TreeNode*>& result){
if(root!=NULL){
result.push_back(root);
preorder(root->left, result);
preorder(root->right, result);
}
}
void flatten(TreeNode* root) {
vector<TreeNode*> result;
if(root==NULL)
return;
preorder(root, result);
for(int i=0;i<result.size()-1;i++){
result[i]->right=result[i+1];
result[i]->left=NULL;
}
result[result.size()-1]->left= result[result.size()-1]->left = NULL;
}
};
//Method 2
/*For a subtree, we store the root's right child in a temporary variable and store root's left child in root->right
Then we traverse to the rightmost child of the root and make that child's right as the temporary variable.
Consider this subtree:
1
/ \
2 3
We store node 3 in a temporary variable. Then we make node 2 as the right child of root node, 1
Then we go to the rightmost node from root, that is 2, and make the right node of 2 as the node in the temp variable, ie 3
*/
TreeNode* getRightMost(TreeNode* root){
// if(root==NULL) return root;
if(root->right==NULL) return root;
return getRightMost(root->right);
}
void flatten(TreeNode* root) {
if(root==NULL)
return;
flatten(root->left);
flatten(root->right);
TreeNode* temp = root->right;
root->right = root->left;
root->left=NULL;
TreeNode* rightnode = getRightMost(root);
rightnode->right = temp;
}