0 ms c++ solution that corrupt the tree though!


  • 0
    R
    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode* root) {
            vector<int> v;
            if(root == NULL) {
                return v;
            }
            
            stack<TreeNode*> s;
            s.push(root);
            TreeNode* node;
            while(!s.empty()) {
                node = s.top();
                if(node->left == NULL && node->right == NULL) {
                    v.push_back(node->val);
                    s.pop();
                } else {
                    if(node->right != NULL) s.push(node->right);
                    if(node->left != NULL) s.push(node->left);
                    node->right = NULL;
                    node->left = NULL;
                }
            }
            return v;
        }
    };
    

Log in to reply
 

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