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 ...
随机推荐
- Linux下同步工具inotify+rsync使用详解
1. rsync 1.1 什么是rsync rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.它使用所谓的“Rsync演算法”来使本地和远程两个主机之间的文件达到同步,这 ...
- hibernate 事务的隔离级别 5.1
脏读不可重复读幻读可序列化(符合事务的四个特性的正常情况 ) 解释: 脏读:事务A对数据1做了更新,但是还没有来得及提交 此时事务B对数据1进行了查询获得了事务A更新后的数据, 但是事务A因为一些原因 ...
- perl学习(4) 子程序
子程序,类比c语言中的函数,在形式上个人认为最大的区别:没有形参 1.1.定义子程序 1.2.调用 #! /usr/bin/perl sub marine { $n += 1 ; print &quo ...
- VC中TRACE()的用法
个人总结:最近看网络编程是碰到了TRACE语句,不知道在哪里输出,查了一晚上资料也没找出来,今天终于在CSDN上找到了,真是个高地方啊,方法如下: 1.在MFC中加入TRACE语句 2.在TOOLS- ...
- poj 1664 put apples(dfs)
题目链接:http://poj.org/problem?id=1664 思路分析:数据较小,考虑深度优先搜索搜索解空间. 代码如下: #include <iostream> using n ...
- Block动画 和 Spring动画
Block动画: @interface BlockViewController () @property (weak, nonatomic) IBOutlet UIView *playView; @e ...
- hdu2289Cup(神坑题,精度+二分,以半径二分不能过,以高度为二分就过了)
Problem Description The WHU ACM Team has a big cup, with which every member drinks water. Now, we kn ...
- PHP5新语法学习
Final标记方法,使其不能被子类重载:Final标记类,使其不能被继承. 连续引用返回的对象$obj->method()->method2(); __autoload()使用未定义的类的 ...
- mysql自动备份(windows)
许多时候,为了数据安全,我们的mysql数据库需要定期进行备份,下面介绍两种在windows下自动备份方法: 1.复制date文件夹备份 ============================ 例子 ...
- 在树莓派上设置无线静态IP
修改文件: /etc/network/interfaces,命令如下 sudo nano /etc/network/interfaces 将最后一句iface default inet dhcp,替换 ...