题目描述

根据一棵树的中序遍历与后序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]

返回如下的二叉树:

    3
/ \
9 20
/ \
15 7

解题思路

利用回溯的思想,分别记录生成树时中序遍历和后序遍历对应的段首、段尾,每次构造树时首先构造根节点为后序遍历的尾节点,接着在中序遍历序列中找到根的位置,然后根左对应左子树,根右对应右子树,对应到后序遍历序列中分隔成两段,递归构造左子树和右子树。

代码

 /**
* 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:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return build(inorder, postorder, , inorder.size() - , , postorder.size() - );
}
TreeNode* build(vector<int> inorder, vector<int> postorder, int iLeft, int iRight, int pLeft, int pRight){
if(pLeft > pRight) return NULL;
TreeNode* root = new TreeNode(postorder[pRight]);
int idx = iLeft;
while(inorder[idx] != postorder[pRight]) idx++;
root->left = build(inorder, postorder, iLeft, idx - , pLeft, pLeft + idx - iLeft - );
root->right = build(inorder, postorder, idx + , iRight, pLeft + idx - iLeft, pRight - );
return root;
}
};

LeetCode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)的更多相关文章

  1. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | 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 ...

  2. leetcode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)

    目录 题目描述: 示例: 解法: 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 示例: 给出 中序遍历 inorder = [9,3,15,20,7] ...

  3. [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  4. [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal

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

  5. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  6. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

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

  7. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

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

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

  10. [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 ...

随机推荐

  1. 如何解决Win10电脑网速慢的问题?

    很多Win10系统用户反映自己的电脑配置并不算低,却比有些旧电脑上网速度还要慢,不知道是什么原因. 其实,除了运营商网速的原因外,我们的电脑设置也会对网速有所影响.下面好系统U盘启动就来介绍一个Win ...

  2. Win7自带的系统备份还原功能如何去使用?

    很多用户都会反映Win7系统使用过程中会出现系统或应用程序方面的小故障,针对这些小问题,再选择进行电脑系统的重装就有些过于麻烦了. 其实Win7系统内带有系统备份和还原的功能,可以在电脑系统出现小问题 ...

  3. Linux基础知识之文件的权限(二)

    除了基本的r,w,x之外,在linux传统的ext2.ext3.ext4文件系统下,还可以设置其他 的文件属性.如chattr,lsattr,而在CentOS7中默认利用xfs作为默认的文件系统,就不 ...

  4. Go语言基础之Cookie和Session

    Cookie和Session Cookie和Session是Web开发绕不开的一个环节,本文介绍了Cookie和Session的原理及在Go语言中如何操作Cookie. Cookie Cookie的由 ...

  5. win10电脑配置

    微信 QQ 电脑管家 Chrome 坚果云 Sublime VLC 网易云音乐 Acrobat Reader DC PS git potplayer TeamViewer 有道云笔记/协作 百度网盘/ ...

  6. Elasticsearch索引操作

    一.索引初始化操作 插件推荐使用head.marvel (收费) 1.1 创建新索引 curl -XPUT 'http://localhost:9200/test' -d ' { "sett ...

  7. for(auto count:counts)

    c++中for(auto count : counts) 这是C++11中的语法,即:Range-based for loop.其中counts应满足:begin(counts), end(count ...

  8. libusb_control_setup

     libusb_fill_control_transfer(transfer, devh, buf, ctrl_urb_complete_cb,             utrans, 1000); ...

  9. 将本地代码使用Git上传更新至Github

    注册.配置git 1. 首先注册git image 2.然后下载.配置git 百度“git下载”,然后默认安装,注意的是最后要添加环境变量,最后安装结果如下: image 配置如下: 1.设置本地的s ...

  10. 第六章 组件 58 组件切换-使用v-if和v-else结合flag进行切换

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...