My solution c++

  • 0
     void preTravel(TreeNode** newRoot, TreeNode*root){
        if (root == NULL)  return;
          (*newRoot)->right = root;
          TreeNode* p_l = root->left;
          TreeNode* p_r = root->right;
          root->left = NULL;
          root->right = NULL;
         *newRoot = root;
          preTravel(newRoot, p_l);
          preTravel(newRoot, p_r);
    void flatten(TreeNode* root) {
          TreeNode newRoot(-1);
          TreeNode* p = &newRoot;
          preTravel(&p, root);
          root = newRoot.right;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.