【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
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的更多相关文章
- 【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 ...
- 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 ...
- 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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【leetcode】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- 【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 ...
随机推荐
- json转换时区问题-------前端展示时间少8小时--解决方法
在application配置文件中加如下: spring.jackson.time-zone=GMT+8
- WIKIOI 1319 玩具装箱
1319 玩具装箱 题目描述 Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维 ...
- github & Front-end JavaScript frameworks
github & Front-end JavaScript frameworks https://github.com/collections/front-end-javascript-fra ...
- 察看linux 发行版
好像没有太通用的方法. 看一下/etc/redhat-release. redhat 系列(包括centos) 会有如下内容 [root@localhost ~]# cat /etc/redhat- ...
- Android: MediaRecorder start failed
在某些机型上,MediaRecorder在调用start方法时,会出现start failed的错误,有一种可能是setVideoFrameRate导致的.要解决这个问题,只需要注释掉这条语句就可以了 ...
- 【Python】Python 标准库 urllib2 的使用细节
转自:http://zhuoqiang.me/python-urllib2-usage.html http://www.cnblogs.com/txw1958/archive/2012/03/12/2 ...
- 【CV论文阅读】 Fast RCNN + SGD笔记
Fast RCNN的结构: 先从这幅图解释FAST RCNN的结构.首先,FAST RCNN的输入是包含两部分,image以及region proposal(在论文中叫做region of inter ...
- 常见的各品牌路由器默认IP地址汇总清单
常见的各品牌路由器默认IP地址汇总清单 下面是各常见品牌路由器的默认IP清单: 如果您使用的设备品牌不在上述列表中,也可以试着访问下RouterIPAddress.com或SetupRouter.co ...
- 输入两个整数n 和m,从数列1,2,3.......n 中任意取几个数, 使其和等于m ,要求将当中全部的可能组合列出来
中兴面试题之中的一个.难度系数中. 题目描写叙述例如以下:输入两个整数n 和m,从数列1,2.3.......n 中任意取几个数, 使其和等于m ,要求将当中全部的可能组合列出来. 逻辑分析: 1.比 ...
- hdu 5078 2014鞍山现场赛 水题
http://acm.hdu.edu.cn/showproblem.php?pid=5078 现场最水的一道题 连排序都不用,由于说了ti<ti+1 //#pragma comment(link ...