[Leetcode series] constructing a binary tree (iterative solution) from the central and post-order traversal sequences)

Source: Internet
Author: User

For a given medium-order traversal of inorder and post-order traversal of postorder, construct a binary tree.

Algorithm idea: Set post-order traversal to Po, and Middle-order traversal to Io.

 

  • First, the last node of the Po is taken as the root node, and the node is added to the STN stack;
  • Then compare the last node of Io with the top node of the STN Stack:
    • If the node is different, add the node to the right of the top node of the stack to the STN stack, and delete the node from the Po;
      • At this time, the stack stores all the right root nodes that have not yet processed the left subtree.
      • When a difference occurs, the depth of the right subtree increases by 1, and the stack depth represents the depth of the current right subtree.
    • If they are the same, first cache the stack top node and delete the IO and stack top elements respectively:
      • If it is still the same (indicating that there is no left subtree in this layer), step 2 is returned;
      • If they are different (the left subtree is included), add the last node of the Po to the left of the P cache node, and add the left subtree to the stack and delete the node from the Po, return to step 2;

Code:

 1 /** 2  * Definition for binary tree 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {13         if(inorder.size() == 0)return NULL;14         TreeNode *p;15         TreeNode *root;16         stack<TreeNode *> stn;17         18         root = new TreeNode(postorder.back()); 19         stn.push(root); 20         postorder.pop_back(); 21         22         while(true)23         {24             if(inorder.back() == stn.top()->val) 25             {26                 p = stn.top();27                 stn.pop(); 28                 inorder.pop_back(); 29                 if(inorder.size() == 0) break;30                 if(stn.size() && inorder.back() == stn.top()->val)31                     continue;32                 p->left = new TreeNode(postorder.back()); 33                 postorder.pop_back();34                 stn.push(p->left);35             }36             else 37             {38                 p = new TreeNode(postorder.back());39                 postorder.pop_back();40                 stn.top()->right = p; 41                 stn.push(p); 42             }43         }44         return root;45     }46 };

 

[Leetcode series] constructing a binary tree (iterative solution) from the central and post-order traversal sequences)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.