Leetcode 106
/**
* 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) {
return DFS(inorder,,inorder.size()-,postorder,postorder.size()-);
}
TreeNode* DFS(vector<int> inorder,int ileft,int iright,vector<int> postorder,int pos){
if(ileft > iright) return NULL;
int i=;
for(i=ileft;i < iright;i++){
if(postorder[pos] == inorder[i])break;
}
TreeNode* cur = new TreeNode(postorder[pos]);
cur->right = DFS(inorder,i+,iright,postorder,pos-);
cur->left = DFS(inorder,ileft,i-,postorder,pos-iright+i-);
return cur;
}
};
__
Leetcode 106的更多相关文章
- 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 Postorder and Inorder Traversal_Medium tag: Tree Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Leetcode 106. 从中序与后序遍历序列构造二叉树
题目链接 https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/descri ...
- [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 ...
- Java实现 LeetCode 106 从中序与后序遍历序列构造二叉树
106. 从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序 ...
- leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal----- java
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
原题地址 二叉树基本操作 [ ]O[ ] [ ][ ]O 代码: TreeNode *restore(vector<i ...
- 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 ...
随机推荐
- RPC框架原理剖析(含实例)(转)
转自:http://blog.csdn.net/rulon147/article/details/53814589 一.什么是RPC RPC(Remote Procedure Call Protoco ...
- 在Linux 中进入单用户模式的技巧
在这篇简短的文章中,我们将向你介绍在 SUSE 12 Linux 中进入单用户模式的步骤.在排除系统主要问题时,单用户模式始终是首选.单用户模式禁用网络并且没有其他用户登录,你可以排除许多多用户系统的 ...
- Python之路----内置函数补充与匿名函数
内置函数补充:reversed()保留原列表,返回一个反向的迭代器 l = [1,2,3,4,5] l.reverse() print(l) l = [1,2,3,4,5] l2 = reversed ...
- x86,x64,Any CPU区别
https://blog.csdn.net/zuguangboy/article/details/51509670 1,即主程序(编译出来是exe文件的)是x86平台下编译的,而它所依赖的一个项目(或 ...
- Python3 获取网络图片并且保存到本地
Python3 获取网络图片并且保存到本地 import requests from bs4 import BeautifulSoup from urllib import request impor ...
- c++继承、多态以及与java的行为差异之处
对于面向对象而言,多态是最有用的基本特性之一,相对于函数指针,易用得多.下面看下c++继承和多态行为的基本特性,最后说明下和java的基本差别. 首先定义父类和子类. base.h #pragma o ...
- 06: linux下python开发环境梳理
1.1 修改~/.bashrc文件 改编终端颜色 alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # User specific aliases ...
- 20145127《java程序设计》第六周学习总结
教材学习内容总结 第十章 输入与输出 文件的读写 网络上传数据的基础 10.1 InputStream与OutputStream 流(Stream)是对「输入输出」的抽象,注意「输入输出」是相对程序而 ...
- python装饰器,其实就是对闭包的使用。
装饰器 理解装饰器要先理解闭包(在闭包中引用函数,可参考上一篇通过例子来理解闭包). 在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator). 装饰器的实质就是对闭包的使用,原函数被 ...
- python面向对象总结!
面向对象 Object Oriented Programming 基本单元:对象把数据和功能封装在里边,能实现很好的复用性,灵活性和扩展性. 面向对象的两个基本概念:类和对象 面向对象的基本要素:属性 ...