/**
* 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的更多相关文章

  1. 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 ...

  2. (二叉树 递归) 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 ...

  3. [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 ...

  4. Leetcode 106. 从中序与后序遍历序列构造二叉树

    题目链接 https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/descri ...

  5. [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 ...

  6. Java实现 LeetCode 106 从中序与后序遍历序列构造二叉树

    106. 从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序 ...

  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 ...

  8. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. Qt学习之路(45): 自定义model之一

    前面我们说了Qt提供的几个预定义model.但是,面对变化万千的需求,那几个model是远远不能满足我们的需要的.另外,对于Qt这种框架来说,model的选择首先要能满足绝大多数功能的需要,这就是说, ...

  2. Linux学习笔记之如何让普通用户获得ROOT权限

    在学习sodu的时候,我发现一些命令只能由root用户使用,普通用户使用会提示此用户没有使用sudo的权限.我想到的解方法是把正在使用的普通用户获得root权限,于是我通过百度和询问老师知道了如何去实 ...

  3. 20145101《Java程序设计》第4周学习总结

    20145101<Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 继承:避免多个类间重复定义共同行为. 把相同代码提升为父类 运用extends关键字的子类会继承扩 ...

  4. BZOJ 1049 数字序列(LIS)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1049 题意:给出一个数列A,要求:(1)修改最少的数字使得数列严格递增:(2)在( ...

  5. Python 中的几种矩阵乘法 np.dot, np.multiply, *【转】

    本文转载自:https://blog.csdn.net/u012609509/article/details/70230204 Python中的几种矩阵乘法1. 同线性代数中矩阵乘法的定义: np.d ...

  6. Linux deepin 中Jetbrain Idea等软件中文显示异常

    解决方案:安装常用的中文字体 # 文鼎宋体[推荐] sudo apt install fonts-arphic-uming # 文鼎楷体[推荐] sudo apt install fonts-arph ...

  7. 【复制虚拟机】虚拟机复制后无ip的问题

    先编辑虚拟机选项,把网络适配器删掉后保存,再重新添加网络适配器 然后开机 编辑文件/etc/udev/rules.d/70-persistent-net.rules,进去之后是这个样子 把前两个删掉, ...

  8. vscode中使用EF脚手架生成数据库上下文(scaffold-dbcontext)

    目前在vscode上用netcore + ef core,在用dbfirst的方式生成模型和context上下文一直没有找到方法,之前在vs2017中,的nuget管理控制台输入命令: Scaffol ...

  9. Codeforces Beta Round #94 div 2 B

    B. Students and Shoelaces time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  10. pandas 设置单元格的值

    import pandas as pd import pickle import numpy as np dates = pd.date_range() df = pd.DataFrame(np.ar ...