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. [bzoj1569][JSOI2008][Blue Mary的职员分配]

    Description 由于Blue Mary呕心沥血的管理,Blue Mary的网络公司蒸蒸日上.现在一共拥有了n名职员,可惜没有任何的金钱和声誉.平均每名每天职员都可以给公司带来x单位金钱或者y单 ...

  2. js控制frameset的rows

    window.parent.document.getElementById("MainWork").rows="*,0" ;

  3. Java配置方式读取外部的资源配置文件

    通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: package cn.qlq; import org.springframework.context. ...

  4. Memcached高可用方案收集(集群及分布式)

    Memcached的集群方案有很多,不止magent一个,但是单靠集群软件去实现高可用感觉还是会缺少一步,最推荐的方案应该是软件加编码去实现高可用,至少能保证站点的99.5%的可运行行,以下是集群的方 ...

  5. js事件捕获和冒泡解析

    <div id="box"> <div id="box2"> <p id="test">test< ...

  6. [转]《MEF程序设计指南》博文汇总

    在MEF之前,人们已经提出了许多依赖注入框架来解决应用的扩展性问题,比如OSGI 实现以Spring 等等.在 Microsoft 的平台上,.NET Framework 自身内部包含组件模型和 Sy ...

  7. 字段not null属性要放在最后面写,最少在类型后面写

    报错:create table test2211(id not null bigint ,age timestamp); 正确写法:create table test2211(id bigint no ...

  8. mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’

    mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ 今天在linux中安装了mys ...

  9. 一个经典的消费者和生产者的实现(linux )

    #include <stdio.h>   #include <pthread.h>   #define BUFFER_SIZE 16 // 缓冲区数量       struct ...

  10. HDOJ 5383 Yu-Gi-Oh! 最大费用最大流

    网络流裸题: 分两部分建图,求不要求满流的最大费用最大流..... Yu-Gi-Oh! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: ...