106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
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) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        root = NULL;
        if(inorder.empty()) return root;
        root = new TreeNode();
        buildSubTree(inorder,postorder,,inorder.size()-,,postorder.size()-,root);
        return root;
    }
    void buildSubTree(vector<int> &inorder, vector<int>&postorder, int inStartPos, int inEndPos, int postStartPos, int postEndPos,TreeNode * currentNode)
    {
        currentNode->val = postorder[postEndPos]; //后序遍历的最后一个节点是根节点
        //find root position in inorder vector
        int inRootPos;
        for(int i = inStartPos; i <= inEndPos; i++)
        {
            if(inorder[i] == postorder[postEndPos])
            {
                inRootPos = i;
                break;
            }
        }
        //right tree: 是中序遍历根节点之后的部分,对应后序遍历根节点前相同长度的部分
        int newPostPos = postEndPos - max(inEndPos - inRootPos, );
        if(inRootPos<inEndPos)
        {
            currentNode->right = new TreeNode();
            buildSubTree(inorder,postorder,inRootPos+,inEndPos,newPostPos,postEndPos-,currentNode->right);
        }
        //leftTree: 是中序遍历根节点之前的部分,对应后序遍历从头开始相同长度的部分
        if(inRootPos>inStartPos)
        {
            currentNode->left = new TreeNode();
            buildSubTree(inorder,postorder,inStartPos,inRootPos-,postStartPos,newPostPos-,currentNode->left);
        }
    }
private:
    TreeNode* root;
};
106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)的更多相关文章
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
		
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
 - 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
		
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
 - 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: ...
 - 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 Week14]Construct Binary Tree from Inorder and Postorder Traversal
		
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
 
随机推荐
- Java的变量相关
			
变量是在一个范围内的可变的值. 要点: 数据类型(确定变量的值的类型) 一个字节里面8个位,每个位里存储0101这样的二进制的补码表示用来数据,一个字节的数据类型的第一个位是符号位,表示正负. 数据类 ...
 - L183 Chinese company unveils first satellite for free WiFi
			
A Chinese internet technology company unveiled the first satellite in a constellation plan to provid ...
 - memcache+tomcat7.0.37+nginx实现session共享
			
一.session工作原理 由于http是无状态的协议,当我们访问了页面A,然后访问页面B,http无法确定这2个页面的访问是来自同一个人.因此,我们要用cookie或session来跟踪用户,根据授 ...
 - js实现trim()方法
			
在面向对象编程里面去除字符串左右空格是很容易的事,可以使用trim().ltrim() 或 rtrim(),在jquery里面使用$.trim()也可以轻松的实现.但是在js中却没有这个方法.下面的实 ...
 - 查看nvidia显卡命令
			
http://blog.csdn.net/luo123n/article/details/50296973 nvidia-smi
 - python functiontools 模块
			
一个内置的模块. 作用是实现了更多的功能, 同时形式上显得很简洁. 虽然在使用很方便, 但其中的原理还是很难复杂的. ------------------------------------- ...
 - Pandas dataframe 标记删除重复记录
			
Pandas提供了duplicated.Index.duplicated.drop_duplicates函数来标记及删除重复记录 duplicated函数用于标记Series中的值.DataFrame ...
 - cratedb 基本试用
			
安装 docker run -d -p 4200:4200 crate UI访问 http://localhost:4200/#!/ 创建数据 tweets 是默认导入的,点击帮助导航可以操作 登陆 ...
 - Linux wc指令解析
			
wc指令比较实用,可以统计文件中的字节数.字符数.行数.字数等. 先通过 wc --help 查看指令帮助. $ wc --help Usage: wc [OPTION]... [FILE]... o ...
 - SQL Server 查询表的主键的两种方式
			
方式1: select b.column_name from information_schema.table_constraints a inner join information_schema. ...