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 ...
随机推荐
- boost之内存池
讲到内存池我们会想到对对象进行动态分配的过程new包含三个过程 1.使用operator new分配内存 2.使用placement new 初始化 3.返回内存地址. 分配内存可以分解成分配内存和获 ...
- python 里安装 tensorflow 后运行出错的问题解决
如果出现一下错误: libcublas.so.8.0: cannot open shared object file: No such file or directory 原因是没有 cuda 环境, ...
- HDU 1159 Common Subsequence(POJ 1458)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- jQuery:自学笔记(3)——操作DOM
jQuery:自学笔记(3)——操作DOM 修改元素的属性 获取元素属性 设置元素属性 修改元素的内容 说明 有三种方式可以获取HTML元素的内容,分别是 ☐ text():设置或返回所选元素的文本内 ...
- 解决Ubuntun 12.04编译Mesa10.3 WARNING: 'aclocal-1.14' is missing on your system
安 装Mesa时,最后一个错误报“WARNING: 'aclocal-1.14' is missing on your system.”,虽然是个Warning,但是无法进行下一步make,所以必须解 ...
- [RK3288][Android6.0] 调试笔记 --- user版本默认显示开发者选项【转】
本文转载自:https://blog.csdn.net/kris_fei/article/details/70157137 Platform: ROCKCHIPOS: Android 6.0Kerne ...
- 使用fastboot刷机流程【转】
本文转载自:http://www.voidcn.com/blog/Qidi_Huang/article/p-6236224.html [准备工作] 首先需要准备好刷机包,可以是自己编译的,也可以是从别 ...
- LVS 负载均衡原理详解
LVS简介 LVS是一个开源软件,由章文嵩博士于1998年5月创立,可以实现Linux平台下的简单负载均衡.LVS是Linux Virtual Server的简写,是一个虚拟的服务器集群系统. LVS ...
- java 发送http 的get|post 请求
<div> package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import jav ...
- Dom4j quick start guide
Parsing XML Using Iterators Powerful Navigation with XPath Fast Looping Creating a new XML document ...