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,2,3,4,5,6,#}的后序遍历为4->5->2->6->3->1;中序遍历为:4->2->5->1->6->3。

故,递归的整体思路是,找到根节点,然后遍历左右子树。这里有一个很重要的条件是:树中没有重复元素。

方法一:递归

值得注意的是:递归过程中,起始点的选取。Grandyang对下标的选取有较为详细的说明。至于这个递归过程,(个人愚见,欢迎大神给出更好递归过程)可以在脑海想,构造顺序为4->5->2构建好左子树,接上1,然后6->3构建好右子树,最后接上1,完成。

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
int len=inorder.size();
return build(inorder,,len-,postorder,,len-);
}
TreeNode *build(vector<int> &inorder,int inBeg,int inEnd,vector<int> &postorder,int postBeg,int postEnd)
{
if(inBeg>inEnd||postBeg>postEnd) return NULL;
TreeNode *root=new TreeNode(postorder[postEnd]); for(int i=;i<inorder.size();++i)
{
if(inorder[i]==postorder[postEnd])
{
root->left=build(inorder,inBeg,i-,postorder,postBeg,postBeg+i--inBeg);
root->right=build(inorder,i+,inEnd,postorder,postBeg+i-inBeg,postEnd-);
}
}
return root;
}
};

方法二:

利用栈。这个方法是以前看到出处也尴尬的忘了,分析过程等我再次看懂了再补上。这种方法改变了数组。

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
if(inorder.size()==) return NULL;
TreeNode* p;
TreeNode* root;
stack<TreeNode*> stn; root=new TreeNode(postorder.back());
stn.push(root);
postorder.pop_back(); while(true)
{
if(inorder.back()==stn.top()->val)
{
p=stn.top();
stn.pop();
inorder.pop_back(); if(inorder.size()==) break;
if(stn.size()&&inorder.back()==stn.top()->val)
continue; p->left=new TreeNode(postorder.back());
postorder.pop_back();
stn.push(p->left);
}
else
{
p=new TreeNode(postorder.back());
postorder.pop_back();
stn.top()->right=p;
stn.push(p);
}
}
return root;
}
};

[Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树的更多相关文章

  1. [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 tha ...

  2. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)

    根据中序和后续遍历构建二叉树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...

  3. [LeetCode] 106. 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 ...

  4. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树

    题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序 ...

  6. LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium

    要求:根据中序和后序遍历序列构建一棵二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int ...

  7. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  8. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  9. 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 ...

随机推荐

  1. unity share current game screen

    using UnityEngine; using System.Collections; using UnityEngine.UI; using System.IO; public class Tak ...

  2. TPO-13 C2 How to use language lab

    TPO-13 C2 How to use language lab 第 1 段 1.Listen to a conversation between a student and the languag ...

  3. [CF294B]Shaass and Bookshelf

    问题描述 Shaass拥有n本书.他想为他的所有书制作一个书架,并想让书架的长宽尽量小.第i本书的厚度是t[i],且这本书的纸张宽度是w[i].书的厚度是1或2,所有书都有同样的高度(即书架的高是均匀 ...

  4. 牛客练习赛26:D-xor序列(线性基)

    链接:牛客练习赛26:D-xor序列(线性基) 题意:小a有n个数,他提出了一个很有意思的问题:他想知道对于任意的x, y,能否将x与这n个数中的任意多个数异或任意多次后变为y 题解:线性基 #inc ...

  5. Python全栈 正则表达式(re模块正则接口全方位详解)

    re模块是Python的标准库模块 模块正则接口的整体模式 re.compile 返回regetx对象 finditer fullmatch match search 返回 match对象 match ...

  6. leetcode-前K个高频元素

    给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...

  7. 100. Remove Duplicates from Sorted Array && 101. Remove Duplicates from Sorted Array II [easy]

    这两题类似,所以放在一起,先看第一题: Description Given a sorted array, remove the duplicates in place such that each ...

  8. 有个AI陪你一起写代码,是种怎样的体验?| 附ICLR论文

    从前,任何程序的任何功能,都需要一行一行敲出来. 后来,程序猿要写的代码越来越多,世界上便有了各种各样的API,来减少大家的工作量.有些功能,可以让API来帮我们实现. 不过,人类写下的话,API并不 ...

  9. nordic mesh 任务调度实现

    nordic mesh 任务调度实现 nordic mesh的任务调度室基于定时器实现的,有两个链表结构维护任务. 需要注意的是,任务调度的部分接口只能在"bearer event" ...

  10. .Net并行编程 - 并行任务基础知识

    在微软的.NET Framework中,任务是通过System.Threading.Tasks命令空间中的Task类来实现的.它的静态属性Task.Factory是TaskFactory类的一个实例, ...