Description:

Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and Postorder' (Problem 106), u need build the binary tree.

Input:

105. Preorder & Inorder traversal

106. Inorder & Postorder traversal

output:

A binary tree.

Solution:

  This solution uses the algorithm "divide and conquer". Taking an example of 105, we can get the root node from preorder travelsal then use inorder traversal to divide the range of left tree nodes and the right tree nodes. You can

draw some simple instances on paper,  which will make you totally clear about the thinking.

/**
* 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:
void travel(TreeNode **root)
{
if(*root){
travel(& ((*root) -> left));
cout<<"val is "<<(*root)->val<<endl;
travel(& ((*root) -> right));
}
} TreeNode* createTree(vector<int>& preorder, vector<int>& inorder, int preSta, int preEnd, int inSta, int inEnd)
{
if(preSta > preEnd || inSta > inEnd ) return NULL;
TreeNode *root = new TreeNode(preorder[preSta]);
int index;
for(int i = inSta; i <= inEnd; i ++){
if(inorder[i] == preorder[preSta]){
index = i;
break;
}
}
root -> left = createTree(preorder, inorder, preSta + , preSta + index - inSta, inSta, index - );
root -> right = createTree(preorder, inorder, preSta + index - inSta + , preEnd, index + , inEnd);
return root;
} TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
TreeNode *root = createTree(preorder, inorder, , preorder.size() - , , inorder.size() - );
TreeNode **r = &root;
//travel(r);
return root;
}
}; //106.
class Solution {
public:
TreeNode* createTree(vector<int>& inorder, vector<int>& postorder, int inSta, int inEnd, int postSta, int postEnd){
if(inSta > inEnd || postSta > postEnd)
return NULL;
TreeNode* root = new TreeNode(postorder[postEnd]);
int index;
for(int i = inEnd; i >= inSta; i --){
if(postorder[postEnd] == inorder[i]){
index = i;
break;
}
}
root -> right = createTree(inorder, postorder, index + , inEnd, postEnd - (inEnd - index), postEnd - );
root -> left = createTree(inorder, postorder, inSta, index - , postSta, postSta + (index - inSta - ));
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
TreeNode* root = createTree(inorder, postorder, , inorder.size() - , , postorder.size() - );
return root;
}
};

【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal的更多相关文章

  1. 【LeetCode】105 & 106. Construct Binary Tree from Inorder and Postorder Traversal

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

  2. 105 + 106. Construct Binary Tree from Preorder and Inorder Traversal (building trees)

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

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

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  6. 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  7. 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  8. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  9. 【leetcode】Minimum Depth of Binary Tree (easy)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. Spring核心技术(四)——Spring的依赖及其注入(续二)

    前面两篇文章描述了IoC容器中依赖的概念,包括依赖注入以及注入细节配置.本文将继续描述玩全部的依赖信息. 使用 depends-on 如果一个Bean是另一个Bean的依赖的话,通常来说这个Bean也 ...

  2. Uva12657 Boxes in a Line

    题目链接:传送门 分析:每次操作都会花费大量时间,显然我们只需要关注每个元素的左边是啥,右边是啥就够了,那么用双向链表,l[i]表示i左边的数,r[i]表示i右边的数,每次操作模拟一下数组的变化就好了 ...

  3. mappingLocations、mappingDirectoryLocations与mappingJarLocations 区别 (转)

    mappingLocations.mappingDirectoryLocations与mappingJarLocations 区别 由于spring对hibernate配置文件hibernate.cf ...

  4. 20181010关于pt-kill自动杀死运行超长的进程

    转自: http://blog.chinaunix.net/uid-16844903-id-4442030.htmlhttp://blog.chinaunix.net/uid-31396856-id- ...

  5. The Balance POJ 2142 扩展欧几里得

    Description Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of ...

  6. Ubuntu桌面卡死时的处理

    1.这种方式可以尝试,但是不成功 sudo skill x sudo stop lightdm sudo start lightdm 2.这种方式比较可靠 ps -t tty7 kill 27342 ...

  7. SQL Server 性能优化实战系列(文章索引) : 桦仔

    http://www.cnblogs.com/gaizai/archive/2012/01/20/2327814.html

  8. HDOJ 5402 Travelling Salesman Problem 模拟

    行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...

  9. 在psql客户端中修改函数

    \ef 创建一个新的函数. \df 显示已经创建的函数. \df+    somefunc 显示这个函数的详细定义 \ef   somefunc 编辑这个函数, 编辑保存退出之后,要执行 \g ,刚才 ...

  10. Fragment中的setUserVisibleHint()方法调用

    使用Fragment的时候难免会遇到想在视图可见与不可见之中做些操作.此时一般会想到类似Activity中的onResume()和onPause()方法.Fragment中也确实有这两个方法,然而亲測 ...