Morris Postorder


  • 0
    G

    Morris algorithm can be easily adapted for preorder and inorder traverse. For postorder, we can use reversed preorder traverse, i.e., visit the root, right child and left child. This is exactly the reverse of preorder. So we need to reverse our result before return.

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution
    {
     public:
      vector<int> postorderTraversal(TreeNode* root)
      {
        vector<int> ret;
        for (TreeNode *cur = root, *tmp = nullptr; cur;) {
          if (cur->right) {
            for (tmp = cur->right; tmp->left && tmp->left != cur; tmp = tmp->left)
              ;
            if (tmp->left) {
              tmp->left = nullptr;
              cur = cur->left;
            } else {
              tmp->left = cur;
              ret.push_back(cur->val);
              cur = cur->right;
            }
          } else {
            ret.push_back(cur->val);
            cur = cur->left;
          }
        }
        reverse(ret.begin(), ret.end());
        return ret;
      }
    };
    

Log in to reply
 

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