'Why original root is not changing in main function (Flatten Binary Tree to Linked List)? [duplicate]
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
i am talking about function "flatten", I know i can do it without making another function named help but I want to do this question this way. The help function is flattening the tree correctly and returning it correctly in TreeNode* ans, but it is not reflecting in root, in root it is printing from original
using namespace std;
class TreeNode {
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int val) {
this->val = val;
left = right = nullptr;
}
};
TreeNode *takeInput() {
int rootData;
cout << "Enter data of root node" << endl;
cin >> rootData;
if (rootData == -1) {
return nullptr;
}
TreeNode *root = new TreeNode(rootData);
queue<TreeNode *> pendingNodes;
pendingNodes.push(root);
while (!pendingNodes.empty()) {
TreeNode *front = pendingNodes.front();
pendingNodes.pop();
int leftNodeData;
cout << "Enter val of left child of parent " << front->val << endl;
cin >> leftNodeData;
if (leftNodeData != -1) {
front->left = new TreeNode(leftNodeData);
pendingNodes.push(front->left);
}
int RightNodeData;
cout << "Enter val of Right child of parent " << front->val << endl;
cin >> RightNodeData;
if (RightNodeData != -1) {
front->right = new TreeNode(RightNodeData);
pendingNodes.push(front->right);
}
}
return root;
}
void print(TreeNode *root) {
if (root == NULL) {
return;
}
queue<TreeNode *> pendingNodes;
pendingNodes.push(root);
while (!pendingNodes.empty()) {
TreeNode *front = pendingNodes.front();
pendingNodes.pop();
cout << front->val << ":";
if (front->left != nullptr) {
cout << " L: " << front->left->val;
pendingNodes.push(front->left);
}
if (front->right != nullptr) {
cout << " R: " << front->right->val;
pendingNodes.push(front->right);
}
cout << endl;
}
}
TreeNode *help(TreeNode *root) {
if (root == nullptr) {
return nullptr;
}
TreeNode *leftAns = help(root->left);
TreeNode *rightAns = help(root->right);
if (leftAns != nullptr) {
TreeNode *temp = leftAns;
while (temp->right != nullptr) {
temp = temp->right;
}
temp->right = root;
root->left = nullptr;
}
root->right = rightAns;
if (leftAns != nullptr) {
root = leftAns;
return leftAns;
}
return root;
}
void flatten(TreeNode *root) {
TreeNode *ans = help(root);
// help(root);
// *root = *ans;
root = ans;
// root = help(root);
}
int main() {
TreeNode *root = takeInput();
print(root);
flatten(root);
cout << "ans:" << endl;
print(root);
return 0;
}```
Solution 1:[1]
As user207421 stated in his comment, you are currently passing root into the flatten function by value meaning that when root is assigned the value of ans in the line
root = ans;
Only the local copy of root is being altered. If you passed root into the function by reference, the value of the root value would be changed rather than just a local copy.
The function would simply need to be changed to
void flatten(TreeNode *&root) {
root = help(root);
}
Edit:
Having looked over the leet code link I now realize the previous answer is invalid for doesn't exactly match the provided flatten function signature given in the challenge. I will leave the original answer above in case it is useful. If you wish to change the value of root, you should assign the value pointed to by answer to the memory location pointed to be root
void flatten(TreeNode *root) {
*root = *help(root);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
