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 duplicates do not exist in the tree.

递归构建。

思路就是: preorder可以定位到根结点,inorder可以定位左右子树的取值范围。

1. 由preorder得到根结点;把preorder第一个点删掉;

2. 先建左子树;再建右子树;

通过一个区间来表示左右子树的取值范围。因为inorder左右子树的范围都是连续的。中间就是root。

  1. class Solution {
  2. public:
  3. TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
  4. return recursive(preorder, inorder, , inorder.size() - );
  5. }
  6.  
  7. TreeNode* recursive(vector<int> &preorder, vector<int> &inorder, int s, int e) {
  8. if (s > e) return NULL;
  9. if (preorder.empty()) return NULL;
  10. TreeNode *root = new TreeNode(preorder.front());
  11. preorder.erase(preorder.begin());
  12.  
  13. int i = s;
  14. for (; i <= e && inorder[i] != root->val; ++i);
  15. root->left = recursive(preorder, inorder, s, i - );
  16. root->right = recursive(preorder, inorder, i + , e);
  17. }
  18. };

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. postorder,最后一个元素是根结点。

2. 先构建右子树,再构建左子树。

  1. class Solution {
  2. public:
  3. TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
  4. return recursive(postorder, inorder, , inorder.size() - );
  5. }
  6.  
  7. TreeNode* recursive(vector<int> &postorder, vector<int> &inorder, int s, int e) {
  8. if (s > e) return NULL;
  9. if (postorder.empty()) return NULL;
  10. TreeNode *root = new TreeNode(postorder.back());
  11. postorder.pop_back();
  12.  
  13. int i = s;
  14. for (; i <= e && inorder[i] != root->val; ++i);
  15. root->right = recursive(postorder, inorder, i + , e);
  16. root->left = recursive(postorder, inorder, s, i - );
  17. }
  18. };

Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal的更多相关文章

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

  2. 105. Construct Binary Tree from Inorder and preorder Traversal

    /* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root ...

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

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

  4. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

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

  6. [LeetCode] Construct Binary Tree from Inorder and Pretorder Traversal

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

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

  8. [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...

  9. [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树

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

随机推荐

  1. MapReduce实现单词统计

     开发工具:IDEA mapreduce实现思路: Map阶段: a) 从HDFS的源数据文件中逐行读取数据 b) 将每一行数据切分出单词 c) 为每一个单词构造一个键值对(单词,1) d) 将键值对 ...

  2. Linuxshell编程

    重点回顾: 1.由于核心的内存中是受保护的区块,因此我们必须要透过“shell”将我们输入的指令与Kernel沟通,好让Kernel可以控制硬件来正确无误的工作 2.学习shell的原因主要有:文字接 ...

  3. mysql中外联和 is null 结合使用

    今天学习mysql ,碰到了一个问题:有部门表,员工表,员工表中有一个部门表的外键,查询没有员工的部门名称. 表结构如下: 员工表employees: 部门表department表: 题目很简单呢,信 ...

  4. dcpromo(server2012不支持)

    dcpromo 编辑 dcpromo命令是一个“开关”命令.如果Windows 2000 Server计算机是成员服务器,则 运行dcpromo命令会安装活动目录,将其升级为域控制器:如果Window ...

  5. Java资料整理

    Java资料整理 原创 2017年08月25日 17:20:44 14211  1.LocalThread的应用场景,数据传输适合用LocalThread么 2.linux的基本命令    软链接.更 ...

  6. c语言入门-02-第一个c程序开始

    我们来开我们第一个c代码 #include<stdio.h> int main(){ // print num int num; num = 1; printf("%d\n&qu ...

  7. CSU-2220 Godsend

    题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2220 题目 Description Leha somehow found ...

  8. 练习题 - js函数

    代码贴出来 1 function Cat() { 2 getColor = function(){ console.log(1);} 3 return this; 4 } 5 Cat.getColor ...

  9. SEO相关

    前端需要注意哪些SEO 合理的title.description.keywords: -- 搜索对着三项的权重逐个减小,title值强调重点即可,重要关键词出现不要超过2次,而且要靠前,不同页面tit ...

  10. java简易DVD影片管理系统—面向对象

    public class DvdSet { String name [] =new String[15]; // DVD电影名称 String date [] =new String[15]; //D ...