73_leetcode_Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree
1:中序和后序遍历构成一棵树。2:採用递归的方法。3:把两个数组分别分成两部分;4:注意递归结束情况
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
if(inorder.size() == 0 || postorder.size() == 0 || inorder.size() != postorder.size())
{
return NULL;
} int size = (int)inorder.size();
return buildTreeCore(inorder, 0, postorder, 0, size);
} TreeNode *buildTreeCore(vector<int> &inorder, int inStart, vector<int> &postorder, int postStart, int length)
{
if(length == 1)
{
if(inorder[inStart] != postorder[postStart])
{
return NULL;
}
} int rootValue = postorder[postStart + length - 1];
TreeNode *root = new TreeNode(rootValue); int i = 0;
for(; i < length; i++)
{
if(inorder[inStart + i] == rootValue)
{
break;
}
} if(i == length)
{
return NULL;
} if(i > 0)
{
root->left = buildTreeCore(inorder, inStart, postorder, postStart, i);
} if(i < length - 1)
{
root->right = buildTreeCore(inorder, inStart + i + 1, postorder, postStart + i, length - i - 1);
} return root;
}
73_leetcode_Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [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_Construct Binary Tree from Inorder and Postorder Traversal
一.题目 Construct Binary Tree from Inorder and Postorder Traversal My Submissions Given inorder and pos ...
随机推荐
- Qt之QNetworkInterface(查询网络接口),QHostInfo(查询主机IP)
http://blog.csdn.net/u011012932/article/details/50775052 http://blog.csdn.net/u011012932/article/det ...
- fedora audacious 不能播放音乐
采用命令sudo dnf search audacious 可以看到 audacious-plugins-freeworld-mp3.x86_64 : MP3 playback plugin for ...
- Maven--几个需要补充的问题(三)
<Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> 前两篇由于篇幅太长,为了给读者理解方便,这篇 ...
- 读懂Swift 2.0中字符串设计思路的改变
Swift提供了一种高性能的,兼容Unicode编码的String实现作为标准库的一部分.在 Swift2中,String类型不再遵守CollectionType协议.在以前,String类型是字符的 ...
- BZOJ 1455: 罗马游戏( 配对堆 + 并查集 )
可并堆水题 --------------------------------------------------------- #include<bits/stdc++.h> usin ...
- python成长之路——第三天
一.collections系列: collections其实是python的标准库,也就是python的一个内置模块,因此使用之前导入一下collections模块即可,collections在pyt ...
- java--String方法
String : 字符串类型 一.构造函数 String(byte[ ] bytes):通过byte数组构造字符串对象. String(char[ ] value):通过char数组构造字符串对象. ...
- iOS判断字符串是否包含表情字符
- (BOOL)isContainsEmoji:(NSString *)string { __block BOOL isEomji = NO; [, [string length]) options: ...
- 11g的alert日志路径
一个测试库,11g,没有sys账户,无法用show parameter dump查看alert日志的路径,以前也碰到过,但后来就不了了之了.这次深挖下,也参考了下一些网上的帖子,于是找到了: $ORA ...
- 测试DOM0级事件和DOM2级事件的堆叠
1. 问题 如果大家看过北风网CJ讲师的Javascript视频教程,就可以看到其封装了一个很强的事件添加和删除函数,如下所示 function addEvent(obj, evtype, fn) { ...