leetcode 106. Constructing a binary tree from inorder and postorder traversal sequences

Construct a binary tree based on inorder traversal and postorder traversal of a tree.

Note:
You can assume that there are no duplicate elements in the tree.

For example, given

Inorder traversal inorder = [9,3,15,20,7]
Postorder traversal postorder = [9,15,7,20,3]

Returns the following binary tree:

3
   / \
  9  20
    /  \
   15 7 

At first, I didn't understand the meaning of constructing a tree. Before this type of question, all I was exposed to were returning an array of a certain traversal order. Later, after referring to other people's answers,
I realized that constructing a tree is actually It is to build a tree in the form of a linked list and return to the root node
to solve the problem of the tree. The basic idea is to use recursive
in-order traversal:
  1. visit the root node
  2. Inorder traversal of the left node of the root node
  3. Inorder traversal of the right node of the root node

Post-order traversal:

  1. Traverse the left subtree of the root node in postorder
  2. Post-order traversal of the root node's right subtree
  3. visit the root node

Tree traversal is achieved by recursive thinking

Inorder traversal inorder = [9,3,15,20,7]
Postorder traversal postorder = [9,15,7,20,3] 
3
   / \
  9  20
    /  \
   15 7 
By observing the results of the traversal, it can be found that the last traversal of the post-order traversal is the root node. In the in-order traversal,
the left subtree of the tree is to the left of the root node, and the right subtree of the tree is to the right of the root node
. In the example, the last one of the post-order traversal is 3, then in the in-order traversal, 9 is the left subtree of the tree, 15, 20, and 7 are the left subtree obtained from the right subtree of the tree
according to the previous step, and the right subtree The length of the post-order traversal is divided into left subtree and right subtree according to the length.
Perform the same traversal as above and
show the recursive implementation process
dfs(inorder, postorder, 0, 4, 0, 4)
  root->val = postorder[4] = 9
  i = 1;
  root->left = dfs( inorder, postorder, 0, 0, 0, 0)
      root->val = postorder[0] = 9;
      i = 0;
root->left = dfs(inorder, postorder, 0, -1, 0, -1)
              return null;
    root->right = dfs(inorder, postorder,1, 0, 1, 0)
              return null;
  root->right = dfs(inorder, postorder, 2, 4, 1, 3)
    root->val = postorder[ 3] = 20
    i = 3
    root->left = dfs(inorder, postorder, 2, 2, 1, 1)
      root->val = postorder[1] = 15
      i = 2;
      root->left = dfs(inorder, postorder, 2, 1, 1, 0)
              return null;
      root->right = dfs(inorder, postorder, 3, 2, 2, 1)
              return null;
    root->right = dfs(inorder, postorder, 4, 4, 2, 2)
      root->val = postorder[2] = 7
      i = 4;
      root->left = dfs(inorder, postorder, 4, 3, 2, 1)
              return null
      root->right = dfs(inorder, postorder, 5, 4, 2, 1)
              return null;
  return root;
 1 /**
 2  * Definition for a binary tree node.
 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* dfs(vector<int>& inorder, vector<int>& postorder, int inl, int inr, int postl, int postr){
13         if(inl > inr) return NULL;
14         TreeNode* root = new TreeNode(postorder[postr]);
15         int i = inl;
16         while(inorder[i] != postorder[postr] && i < inr) i++;
17         root->left = dfs(inorder, postorder, inl, i-1, postl, postl+i-inl-1);
18         root->right = dfs(inorder, postorder, i+1, inr, postl+i-inl, postr-1);
19         return root;
20     }
21     TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
22         int l = inorder.size()-1;
23         return dfs(inorder, postorder, 0, l, 0, l);
24     }
25 };

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325125995&siteId=291194637