Question

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:

You may assume that duplicates do not exist in the tree.

Solution

参考:http://www.cnblogs.com/zhonghuasong/p/7096150.html

Code

/**
* 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>& preorder, vector<int>& inorder) {
if (preorder.size() == 0 || inorder.size() == 0)
return NULL; return ConstructTree(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
} TreeNode* ConstructTree(vector<int>& preorder, vector<int>& inorder,
int pre_start, int pre_end, int in_start, int in_end) {
int rootValue = preorder[pre_start];
TreeNode* root = new TreeNode(rootValue);
if (pre_start == pre_end) {
if (in_start == in_end)
return root;
} int rootIn = in_start;
while (rootIn <= in_end && inorder[rootIn] != rootValue)
rootIn++; int preLeftLength = rootIn - in_start;
if (preLeftLength > 0) {
root->left = ConstructTree(preorder, inorder, pre_start + 1, preLeftLength, in_start, rootIn - 1);
}
// 左子树的对大长度就是in_end - in_start
if (preLeftLength < in_end - in_start) {
root->right = ConstructTree(preorder, inorder, pre_start + 1 + preLeftLength, pre_end, rootIn + 1, in_end);
} return root;
}
};

LeetCode——Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

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

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

  4. [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

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

  5. Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal

    总结: 1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标 代码: 前序中序 #include <iostream> #include <vector> usi ...

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

  7. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  8. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

  9. 【题解二连发】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 - LeetCode Construct Binary ...

随机推荐

  1. adb连接夜神模拟器

    1.打开夜神模拟器,打开设置,调成手机模式,初次进入的话,进入设置,点击版本号5次,可以激活使用开发者模式,进入后打开USB调试功能 2.打开文件资源管理器,进入夜神模拟器的安装位置,在地址栏输入cm ...

  2. StringUtils 时间显示,判断手机号,电子邮件,是否为今日,是否空白串,字符串转整数,对象转整数 等

    package com.xiaoyun.org.util; import java.io.BufferedReader; import java.io.IOException; import java ...

  3. 《从零开始学Swift》学习笔记(Day 52)——Cocoa错误处理模式

    原创文章,欢迎转载.转载请注明:关东升的博客 Swift错误处理模式,在Swift 1.x和Swift 2.0是不同的两种模式. Swift 1.x代码错误处理模式采用Cocoa框架错误处理模式,到现 ...

  4. inner join和out join的区别

    inner join(又叫join) out join包括left join,right join和full join(也就是left+right)

  5. 是面向对象设计五个基本原则(SOLID)

    单一职责原则 - 搜狗百科 https://baike.sogou.com/v51360965.htm 单一职责原则并不是一个孤立的面向对象设计原则,它是面向对象设计五个基本原则(SOLID)之一.这 ...

  6. 【python】-- Ajax

    Ajax AJAX,Asynchronous JavaScript and XML (异步的JavaScript和XML),一种创建交互式网页应用的网页开发技术方案. 异步的JavaScript:使用 ...

  7. VMware虚拟机安装(二)

    我们一般用户使用的是windows ,那如何装一个Centos呢,我们一般学习的话,是搞一个VM虚拟机, 在虚拟机里安装一个Centos操作系统来学习,当然正式部署的话 我们一般是从阿里云或者西部数码 ...

  8. Oracle数据库使用Analyze提升sql性能

    Oracle数据库使用Analyze提升sql性能 如果你不使用analyze完成sql优化,将意味着:你拒绝使用数据库的查询优化器,也失去了使用优化连接的机会.假设你创建了一张拥有100万条记录的临 ...

  9. Python3.6全栈开发实例[019]

    19.干掉主播.现有如下主播收益信息, 按照要求, 完成相应操作:(1)计算主播平均收益值 sum = 0 for i in zhubo.values(): sum +=i print(round(s ...

  10. 0102-使用 API 网关构建微服务

    一.移动客户端如何访问这些服务 1.1.客户端与微服务直接通信[很少使用] 从理论上讲,客户端可以直接向每个微服务发送请求.每个微服务都有一个公开的端点(https ://.api.company.n ...