Created
July 3, 2023 13:52
-
-
Save SuryaPratapK/69c8c8e5be13b5d03dc09c50a95d52f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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