【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 ...
随机推荐
- 清空所有Session
//清空所有Session request.getSession().invalidate();
- NIO基础学习——缓冲区
NIO是对I/O处理的进一步抽象,包含了I/O的基础概念.我是基于网上博友的博客和Ron Hitchens写的<JAVA NIO>来学习的. NIO的三大核心内容:缓冲区,通道,选择器. ...
- tomcat服务器配置把Http协议强制转化为Https
1)在命令提示符窗口,进入Tomcat目录,执行以下命令: keytool -genkey -alias tomcat -keyalg RSA -keypass changeit -storepass ...
- Ubuntu 16.04安装uGet替代迅雷,并在Chrome中设置为默认下载器
uGet是采用aria2作为下载后端,所以两个软件都必须同时安装. 1.安装uGet sudo add-apt-repository ppa:plushuang-tw/uget-stable sudo ...
- 11、Java并发性和多线程-Java内存模型
以下内容转自http://ifeve.com/java-memory-model-6/: Java内存模型规范了Java虚拟机与计算机内存是如何协同工作的.Java虚拟机是一个完整的计算机的一个模型, ...
- Attempting to track I/O with systemtap
https://glandium.org/blog/?p=1476 Attempting to track I/O with systemtap There are several ways a pr ...
- 两张图让git新手在项目中运用git命令行
创建分支命令: git branch (branchname) 切换分支命令: git checkout (branchname) 当你切换分支的时候,Git 会用该分支的最后提交的快 ...
- Codeforces 577E Ann and Half-Palindrome 字典树
题目链接 题意: 若一个字符串是半回文串.则满足第一位和最后一位相等, 第三位和倒数第三位相等.如此类推. 给定一个字符串s,输出s的全部子串中的半回文串字典序第k大的 字符串. good[i][j] ...
- java中commons-beanutils的介绍
1. 概述 commons-beanutil开源库是apache组织的一个基础的开源库.为apache中很多类提供工具方法.学习它是学习其它开源库实现的基础. Commons-beanutil中包 ...
- nmap,port扫描,获取sshserver的ip地址
// 查看局域网的ip地址 arp - a // 同一个网段.假设用虚拟机桥接则不行 sudo nmap -sS 192.168.1.* //或者sudo nmap -sS -p 22 192.168 ...