[Help] This code works well locally, but got RE in LeetCode


  • 0
    E
      vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        TreeNode *p = root, *tmp = nullptr;
        while (p) {
          res.push_back(p->val);
          if (p->left) {
            tmp = p->left;
            while (tmp->right) tmp = tmp->right;
            tmp->right = p->right;
            p = p->left;
          }
          else {
            p = p->right;
          }
        }
        return res;
      }

  • 0
    R
    This post is deleted!

Log in to reply
 

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