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 ...
随机推荐
- 微信公众号的搭建-第四天(2)-获取并缓存access_token
1. 什么是access_token? 为了使第三方开发者能够为用户提供更多更有价值的个性化服务,微信公众平台 开放了许多接口,包括自定义菜单接口.客服接口.获取用户信息接口.用户分组接口.群发接口等 ...
- English Phrases
@1:Phrases requst sth from/of sb 向某人要求某物 a new lease on life 重获新生.焕发生机 state of the art 最先进的 at th ...
- 编译安装 nginx的http_stub_status_module监控其运行状态
步骤: 1 编译nginx,加上参数 --with-http_stub_status_module 以我自己的编译选项为例: #配置指令 ./configure --prefix=/usr/local ...
- 在html中插入音频
在html中插入音频 第一种:在页面代码中的<head></head>之间加入<bgsound src="音乐url" loop="-1&q ...
- RHEL 5 安装gcc
rpm -ivh kernel-headers... rpm -ivh glibc-headers... rpm -ivh glibc-devel... rpm -ivh libgomp.. rpm ...
- PHP汉子转拼音
<?php /** +------------------------------------------------------ * PHP 汉字转拼音 +------------------ ...
- TIJ读书笔记02-控制执行流程
TIJ读书笔记02-控制执行流程 TIJ读书笔记02-控制执行流程 if-else 迭代 无条件分支 switch语句 所有条件语句都是以条件表达式的真假来决定执行路径,也就是通过布尔测试结果来决 ...
- 离乡与理想 Demo
离乡与理想---理想不分黑白 踏上了火车,离开家乡背着幻梦,站累了有人叫我幸福,叫我快乐而我是没有名字的流浪的日子,已数不清有多少带着理想,一路飞翔路上遇见困难,一定要坚强因为理想已离乡 我为理想而离 ...
- 写给后端程序员的HTTP缓存原理介绍--怎样决定一个资源的Cache-Control策略呢
通过Internet获取资源既缓慢,成本又高.为此,Http协议里包含了控制缓存的部分,以使Http客户端可以缓存和重用以前获 取的资源,从而优化性能,提升体验.虽然Http中关于缓存控制的部分,随着 ...
- TypeScript手册1 - 基本类型和接口
基本类型 像基本的JavaScript一样,TypeScript支持numbers, strings, structures, boolean等基本类型,而扩展的enum等类型,为编程提供了更多帮助. ...