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 ...
随机推荐
- 转:C++:从子类访问父类的私有函数
众所周知,c和c++的数组都是不安全的,因为无论c还是c++都不提供数组边界检查功能,这使得数组溢出成为可能.从某个意义上说,c和c++是一种缺少监督的语言,然而这也正是其魅力所在.c++给予程序员更 ...
- Nginx 开启 debug 日志的办法
译序:一般来讲,Nginx 的错误日志级别是 error,作为 Nginx 用户来讲,你设置成 info 就足够用了. 但有时有些难以挖掘的 bug,需要看到更详细的 debug 级别 ...
- cocos2dx进阶学习之场景切换
背景 在学习马里奥时,我们学习到从菜单场景到游戏场景的切换,代码如下 void CMMenuScene::OnStartCallBack( CCObject *pSender ) { CCDirect ...
- Java学习之二分查找算法
好久没写算法了.只记得递归方法..结果测试下爆栈了. 思路就是取范围的中间点,判断是不是要找的值,是就输出,不是就与范围的两个临界值比较大小,不断更新临界值直到找到为止,给定的集合一定是有序的. 自己 ...
- 最小值滤波 (C 语言实现)
最小值滤波 (C 语言实现) 遇到最小值滤波的问题,小白不知道.一个程序写了三天,最终今天傍晚出来了. .. 非常easy的for循环.可是没有理解最小值滤波.怎么写都是错啊~ 这是我见过做好的描写叙 ...
- ThinkPHP - F函数,更新配置文件
Html代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&quo ...
- iOS 开发设计常用软件及工具整理
1, xCode 2, AppCode 3, Skech 原型设计软件 4, Hype 动画设计工具 5, fontawsome 免费图表 6, Prepo icon, images.catlog 生 ...
- Python 做过哪些有趣的项目
1 icedx 241 天前 via Android ♥ 1 考虑到Windows 下的类Alfred 软件都太傻逼 自己用PyQT 写了一个 2 crazyxin19 ...
- Dreamer2.1 发布 新增将Bean解析成xml和json
一个上午,增加两个功能 1.直接将对象解析成XML 2.将对象解析成JSON 对象可以是数组,可以是集合,也可以是单个对象 源码和jar下载地址:http://pan.baidu.com/share/ ...
- Android中使用"running services"查看service进程内存
从Android 2.0开始,在Settings中加入了一个新的activity("Running Services" activity),它用于显示当前运行的每个Services ...