LeetCode OJ:Construct Binary Tree from Inorder and Postorder Traversal(从中序以及后序遍历结果中构造二叉树)
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
同样是给出了不会有重复数字的条件,用递归较容易实现,代码如下:
/**
* 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>& inorder, vector<int>& postorder) {
if(inorder.size() == )
return NULL;
return createTree(inorder, , inorder.size() - , postorder, , postorder.size() - );
} TreeNode* createTree(vector<int> & inorder, int inBegin, int inEnd,
vector<int> & postorder, int postBegin, int postEnd)
{
if(inBegin > inEnd) return NULL;
int rootVal = postorder[postEnd];
int mid;
for(int i = inBegin; i <= inEnd; ++i){
if(inorder[i] == rootVal){
mid = i;
break;
}
}
int len = mid - inBegin;
TreeNode * left = createTree(inorder, inBegin, mid - , //边界条件同样应该注意
postorder, postBegin, postBegin + len - );
TreeNode * right = createTree(inorder, mid + , inEnd,
postorder, postBegin + len, postEnd - );
TreeNode * root = new TreeNode(rootVal);
root->left = left;
root->right = right;
return root;
}
};
java版本的代码如下所示,没有区别:
public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length == 0)
return null;
return createTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
} public TreeNode createTree(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd){
if(inEnd < inBegin)
return null;
int rootVal = postorder[postEnd];
int mid = 0;
for(int i = inBegin; i <= inEnd; ++i){
if(inorder[i] == rootVal){
mid = i;
break;
}
}
int len = mid - inBegin;
TreeNode leftNode = createTree(inorder, inBegin, mid - 1, postorder, postBegin, postBegin + len - 1);
TreeNode rightNode = createTree(inorder, mid + 1, inEnd, postorder, postBegin + len, postEnd - 1);
TreeNode root = new TreeNode(rootVal);
root.left = leftNode;
root.right = rightNode;
return root;
}
}
LeetCode OJ:Construct Binary Tree from Inorder and Postorder Traversal(从中序以及后序遍历结果中构造二叉树)的更多相关文章
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- (二叉树 递归) leetcode 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 that ...
- [LeetCode] 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 that ...
- LeetCode 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 that ...
- C#解leetcode 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 that ...
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)
原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...
随机推荐
- Facebook支持python的开源预测工具Prophet
Facebook 宣布开源一款基于 Python 和 R 语言的数据预测工具――“Prophet”,即“先知”.取名倒是非常直白. Facebook 表示,Prophet 相比现有预测工具更加人性化, ...
- iOS应用生命周期
作为应用程序的委托对象,AppDelegate类在应用生命周期的不同阶段会回调不同的方法.首先,让我们先了解一下iOS 应用的不同状态及它们彼此间的关系,见图1 . 图1 iOS应用状态图 下面简要介 ...
- JAVA学习笔记----【转】 java.toString() ,(String),String.valueOf的区别
在java项目的实际开发和应用中,常常需要用到将对象转为String这一基本功能.本文将对常用的转换方法进行一个总结. 常用的方法有Object#toString(),(String)要转换的对象,S ...
- yii2-lock-form 也许这就是你想要的,阻止表单多次提交
是不是被用户的行为所困扰? 一个表单用户点击提交按钮了N次,这也导致了数据提交了N次. 为了此受到了测试的欺辱,受到了老板的批评? 不用怕,它就是来拯救你的. 第一步:打开命令行,敲入 compose ...
- PHP用星号隐藏部份用户名、身份证、IP、手机号、邮箱等实例
一.仿淘宝评论购买记录隐藏部分用户名,以下代码亲测可用. function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') { if( ...
- lelel-5
一.样式有几种引入方式?link和@import有什么区别? 样式有3种引入方式: 外部样式(外联式Linking):是将网页链接到外部样式表<link rel="stylesheet ...
- h5新特性 File API详解
之前一直觉得h5的新特性就是一些新标签呢,直到想研究一下图片上传预览的原理,才发现还是有好多新的api的,只是不兼容ie低版本,挺可惜的, File API在表单中文件输入字段基础上,又添加了一些直接 ...
- Java Collection API
在 Java2中,有一套设计优良的接口和类组成了Java集合框架Collection,使程序员操作成批的数据或对象元素极为方便.这些接口和类有很多对抽象数据类型操作的API,而这是我们常用的且在数据结 ...
- win7 与 Ubuntu 16.04 文件传送
win7 与 Ubuntu 16.04 文件传送 环境:主机系统为win7,虚拟机为vmware12, 虚拟系统为ubuntu 16.04 方案一: 通过虚拟机vmware的共享文件夹实现. 方案二: ...
- JavaWeb Cookie
1. Cookie 1.1. Cookie概述 Cookie译为小型文本文件或小甜饼,Web应用程序利用Cookie在客户端缓存服务器端文件.Cookie是以键值对形式存储在客户端主机硬盘中,由服务器 ...