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

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

解题思路:

给出一个二叉树的先序和中序遍历结果,还原这个二叉树。

对于一个二叉树:

         1
/ \
2 3
/ \ / \
4 5 6 7

先序遍历结果为:1 2 4 5 3 6 7

中序遍历结果为:4 2 5 1 6 3 7

由此可以发现规律:

1、先序遍历的第一个字符,就是根结点(1)

2、发现根节点后,对应在中序遍历中的位置,则在中序遍历队列中,根节点左边的元素构成根的左子树,根的右边元素构成根的右子树;

3、递归的将左右子树也按照上述规律进行构造,最终还原二叉树。

代码:

 /**
* 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) {
return buildSubtree(preorder, inorder, ,
preorder.size()-,
, inorder.size()-);
} TreeNode* buildSubtree(vector<int>& preorder, vector<int>& inorder,
int p_left, int p_right, int i_left, int i_right) {
if (p_left > p_right || i_left > i_right)
return NULL; int root = preorder[p_left];
TreeNode* node = new TreeNode(preorder[p_left]); int range = ;
for (int j = i_left; j <= i_right; ++j) {
if (root == inorder[j]) {
range = j - i_left;
break;
}
} node->left = buildSubtree(preorder, inorder,
p_left + , p_left + range,
i_left, i_left + range - );
node->right = buildSubtree(preorder, inorder,
p_left + range + , p_right,
i_left + range + , i_right);
return node;
}
};

【Leetcode】【Medium】Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

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

  2. 【题解二连发】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 ...

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

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

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

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

  6. Construct Binary Tree from Preorder and Inorder Traversal

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

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

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

  9. 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...

随机推荐

  1. ionic3 cordova 调取软键盘

    应用场景,因为兼容ios,安卓问题,不能直接调用激活软键盘方法.只有在点击按钮时让input框自动获取焦点,激活软键盘.然后把input框定位在键盘上方,软键盘激活可以监听到键盘高度. 先下载keyb ...

  2. restsharp 组件调用返回 gbk 编码的api,中文乱码解决方法。(restsharp response 中文乱码 gbk)

    最近要调一个restful风格的api 用了 一个开源第三方组件,组件还是蛮好用的, 支持直接按参数定义实体类,然后发起请求之前直接 addobject 的方式就把请求参数给添加进去了, 解码的时候可 ...

  3. Oracle 通过子查询批量添加、修改表数据

    1.通过查询快速创建表 create table test1(id,job,mgr,sal) as () ) ---这是一个分页查询 ok,表创建成功 2.通过查询快速创建视图 create or r ...

  4. target与currentTarget区别

    target在事件流的目标阶段:currentTarget在事件流的捕获,目标及冒泡阶段.只有当事件流处在目标阶段的时候,两个的指向才是一样的, 而当处于捕获和冒泡阶段的时候,target指向被单击的 ...

  5. QSS为Qt程序添加不一样的样式

    添加 QSS 样式文件 在 Qt 项目中新建一个或使用已有的 Qt Resource File,在资源文件下面新建一个普通文件,命名为 Light.qss: 为 Light.qss 添加如下内容: 这 ...

  6. HDU 5701 ——中位数计数——————【思维题】

    中位数计数 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  7. step4: Xpath的使用

    XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 包含一个标准函数库. XPath 是一个 W3C 标准. 语法简介: htt ...

  8. bootstrap-select在angular上的应用

    1.bootstrap-select 依赖bootstrap.js ,又依赖jQuery,这些都可以用requirejs来处理. 2.一般bootstrap-select 都放在具体的模块上,而是动态 ...

  9. 开启停止wifi热点bat脚本

    @echo offcolor 2title    启停无线WIFI echo                            启动WIFI=======>按1键   echo        ...

  10. [转]微信小程序-template模板使用

    本文转自:http://blog.csdn.net/u013778905/article/details/59646241 如下图,我在做华企商学院小程序的时候,课程搜索结果页和课程列表页结构是完全一 ...