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 ...
随机推荐
- 并发 错误 java.lang.IllegalMonitorStateException: current thread not owner 分析
public class ThreadTest implements Callable<String> { public String call() throws Exception { ...
- Oracle学习笔记—常用函数
这里记录一些oracle常用的函数. TO_NUMBER()函数 将字符串类型转换成一个 number 类型的值. SELECT TO_NUMBER('100.00') FROM DUAL; TO_C ...
- 20170401 ABAP调用CIS webservice
问题: SAP abap SRM java 调webservice 不通, CIS java 这边的webservice 可以通, WHY? key:请求头,系统框架的问题, LF:因为请求头的 ...
- 升级到tomcat8遇到The method getDispatcherType() is undefined for the type HttpServletRequest
今天升级到tomcat8,发现原来的项目不能运行了,遇到下面的错误:The method getDispatcherType() is undefined for the type HttpServl ...
- Python 1 的数据类型
Python3 中有六个标准的数据类型: Number(数字)String(字符串)List(列表)Tuple(元组)Sets(集合)Dictionary(字典) 1.Number(数字) pytho ...
- ES集群性能调优链接汇总
1. 集群稳定性的一些问题(一定量数据后集群变得迟钝) https://elasticsearch.cn/question/84 2. ELK 性能(2) — 如何在大业务量下保持 Elasticse ...
- IBM的SOA方法论之一——五个切入点和八个场景
一.什么是SOA: 面向服务的体系结构(Service-Oriented Architecture,SOA)是一种 IT 体系结构风格,支持将您的业务转换为一组相互链接的服务或可重复业务任务,可在需要 ...
- windows通过ftp下载linux文件
# windows 下载 linux的文件>> ftp <domain_or_ip>>> <input_username>>> <in ...
- 在Java中调用Python代码
极少数时候,我们会碰到类似这样的问题:与A同学合作写代码, A同学只会写Python,不熟悉Java ,而你只会写Java不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方 ...
- HTTP协议—常见的HTTP响应状态码解析
常见的HTTP响应状态码解析 1XX Informational(信息性状态码) 2XX Success(成功状态码) 3XX Redirection(重定向状态码) 4XX Client Error ...