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。

 class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return recursive(preorder, inorder, , inorder.size() - );
} TreeNode* recursive(vector<int> &preorder, vector<int> &inorder, int s, int e) {
if (s > e) return NULL;
if (preorder.empty()) return NULL;
TreeNode *root = new TreeNode(preorder.front());
preorder.erase(preorder.begin()); int i = s;
for (; i <= e && inorder[i] != root->val; ++i);
root->left = recursive(preorder, inorder, s, i - );
root->right = recursive(preorder, inorder, i + , e);
}
};

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. 先构建右子树,再构建左子树。

 class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return recursive(postorder, inorder, , inorder.size() - );
} TreeNode* recursive(vector<int> &postorder, vector<int> &inorder, int s, int e) {
if (s > e) return NULL;
if (postorder.empty()) return NULL;
TreeNode *root = new TreeNode(postorder.back());
postorder.pop_back(); int i = s;
for (; i <= e && inorder[i] != root->val; ++i);
root->right = recursive(postorder, inorder, i + , e);
root->left = recursive(postorder, inorder, s, i - );
}
};

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. Linux命令之---cd

    命令简介 Linux cd 命令是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 命令格式 cd [目录名] 命令功能 切换当前目录至dirName 常用范例 ...

  2. 给B公司的一些建议(又一篇烂尾的文章)

    感慨:太多太多的悲伤故事,发生在自己身上,发生在自己的身边.因此,为了避免总是走"弯路",走"错误"的道路,最近一直在完善自己的理论模型. 烂尾说明:本文是一篇 ...

  3. python使用@property @x.setter @x.deleter

    @property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的. 1>只有@property表示只读. 2>同时 ...

  4. Redis的概述、优势和安装部署

    Redis概述 Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使 ...

  5. 设计模式之第20章-访问者模式(Java实现)

    设计模式之第20章-访问者模式(Java实现) “嘿,你脸好红啊.”“精神焕发.”“怎么又黄了?”“怕冷,涂的,涂的,蜡.”“身上还有酒味,露馅了吧,原来是喝酒喝的啊.”“嘿嘿,让,让你发现了,今天来 ...

  6. PHP 自定义二维码生成

    环境:PHP 7.*.* ,Composer 包管理工具.QrCode 效果如下: 使用 Composer 安装 QrCode QrCode 类库基于 php 的 GD 库,用于生成任意尺寸的二维码, ...

  7. nyoj 题目12 喷水装置(二)

    喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...

  8. Log4j官方文档翻译(六、日志的级别)

    org.apache.log4j.Level 类提供了下面几种日志级别,你也可以通过继承这些类,自定义级别 ALL 所有日志级别都包括 DEBUG 指定信息事件的粒度是DEBUG,在调试应用的时候会有 ...

  9. ECNU 3263 丽娃河的狼人传说(差分约束)

    丽娃河的狼人传说 Time limit per test: 1.0 seconds Memory limit: 256 megabytes 丽娃河是华师大著名的风景线.但由于学校财政紧缺,丽娃河边的路 ...

  10. 0-Android系统各层中LOG的使用

     Android系统各层中LOG的使用 , ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */ ANDROID_LOG_VERBOSE, ANDR ...