leetcode || 106、Construct Binary Tree from Inorder and Postorder Traversal

problem:

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:

You may assume that duplicates do not exist in the tree.

Hide Tags

Tree Array Depth-first
Search

题意:给定二叉树的中序遍历序列和后续遍历序列,构建这棵二叉树,也很经典

thinking:

(1)这种类型的题,要举个例子找出其规律。对于满二叉树(1,2,3,4,5,6,7),中序遍历:4,2,5,1,6,3,7  后续遍历:4,5,2,6,7,3,1

首先根结点1在后续遍历序列的最后一个位置上,再在中序遍历序列中find 1的位置,1之前的为左子树,1 之后的为右子树。知道子树节点数量后,

同样对于后续遍历序列也可以划分左右子树。

(2)递归重复上述过程

code:

class Solution {
  public:
      TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
          if (inorder.size() == 0)
              return NULL;
          return make(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
      }
  protected:
      template<class it>
      TreeNode *make(it pFirst,it pLast,it qFirst,it qLast)
      {
          if(pFirst==pLast)
              return NULL;
          it loc1 = qLast-1;
          int a = *loc1;
          it loc2=find(pFirst,pLast,a);
          int left_size=loc2-pFirst;
          TreeNode *root=new TreeNode(a);
          root->left=make(pFirst,loc2,qFirst,qFirst+left_size);
          root->right=make(loc2+1,pLast,qFirst+left_size,qLast-1);
          return root;
      }
  };
时间: 2024-10-17 10:29:01

leetcode || 106、Construct Binary Tree from Inorder and Postorder Traversal的相关文章

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

leetcode笔记:Construct Binary Tree from Inorder and Postorder Traversal

一. 题目描述 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 二. 题目分析 这道题和Construct Binary Tree from Preorder and Inorder Traversal类似,都是考察基本概念的,后序遍历是先遍历左子树,然后遍历右子树,最

Leetcode 随笔之 ------ Construct Binary Tree from Inorder and Postorder Traversal

利用一棵二叉树的中序遍历的结果数组和后续遍历的结果数组复原该树: 采用分治策略,解析如下图: 如图:中序遍历数组的division特征为左(0 --> x) 根(x + 1) 右(x + 2 --> length - 1) 后序遍历数组的division特征为左(0 --> x) 根(x + 1 --> length - 2) 右(length - 1) 我们可以将中序和后序的数组的左部分和右部分作为根节点的左子树和右子树进行递归处理 那么如何在这两个数组中找到划分division

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp;amp; Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: 115870 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 解题思路

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中