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. 笔记-网络-抓包-wireshark

    笔记-网络-抓包-wireshark 1.      开始 环境:win8笔记本,无线网 1.1.    无线网卡设置 因为需抓捕无线网卡上的数据包,需要进行一项设置,如捕获有线网卡,无需设置. 打开 ...

  2. 4444: [Scoi2015]国旗计划

    4444: [Scoi2015]国旗计划 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 485  Solved: 232 Description A国 ...

  3. 关于RelativeLayout设置垂直居中对齐不起作用的问题

    直接上代码 1.原有代码:(红色字体部分不起作用,无法让RelativeLayout中的textview居中) <RelativeLayout Android:id="@+id/aut ...

  4. Apache shiro学习总结

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  5. leetcode 【 Swap Nodes in Pairs 】python 实现

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...

  6. CentOS7 haproxy+keepalived实现高可用集群搭建

    一.搭建环境 CentOS7 64位 Keepalived 1.3.5 Haproxy 1.5.18 后端负载主机:192.168.166.21 192.168.166.22 两台节点上安装rabbi ...

  7. Ubuntu安装nginx(复制)

    gcc.g++依赖库 apt-get install build-essential apt-get install libtool 安装 pcre依赖库(http://www.pcre.org/) ...

  8. JDBC 学习笔记(十)—— 使用 JDBC 搭建一个简易的 ORM 框架

    1. 数据映射 当我们获取到 ResultSet 之后,显然这个不是我们想要的数据结构. 数据库中的每一个表,在 Java 代码中,一定会有一个类与之对应,例如: package com.gerrar ...

  9. libcmt.lib和msvcrt.lib冲突,原因和解决方法

    libcmt.lib和msvcrt.lib冲突,原因和解决方法 https://blog.csdn.net/longlijun/article/details/7331093 libcmt.lib是w ...

  10. LVS Mode&Method

    LVS NAT 模式: Summary: 普通的NAT模式为DNAT,即只更改目的地址,不改源端口. LVS在转发报文时,将Client的源IP透传给Server,类似于透明传输. 优点: 1. 可提 ...