Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuryaPratapK/69c8c8e5be13b5d03dc09c50a95d52f5 to your computer and use it in GitHub Desktop.
Save SuryaPratapK/69c8c8e5be13b5d03dc09c50a95d52f5 to your computer and use it in GitHub Desktop.
/**
* 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) {}
* };
*/
class Solution {
TreeNode* prev;
void preorder(TreeNode* curr){
if(!curr) return;
if(prev){
prev->left = NULL;
prev->right = curr;
}
TreeNode* r = curr->right;
prev = curr;
preorder(curr->left);
preorder(r);
}
public:
void flatten(TreeNode* root) {
prev = NULL;
preorder(root);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment