My short iterative c++ code using preorder code and adding values to left tree then changing it


  • 0
    G
     void flatten(TreeNode* root) {
            stack<TreeNode* > st;
            TreeNode* temp=root,*prev=NULL,*var,*start=root;
            vector<TreeNode* > ans;
            while(1){
                while(temp!=NULL) {
                    st.push(temp);
                    if(prev!=NULL) prev->left = temp;
                    prev = temp;
                    temp = temp->left;
                }
                if(st.empty()) break;
                temp = st.top();
                st.pop();
                var = temp->right;
                temp = var;
            }
            while(start!=NULL){
                temp = start->left;
                start->right = temp,start->left=NULL;
                start= temp;
            }
        }

Log in to reply
 

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