Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

class Solution {
public:
TreeNode *buildTree(vector<int>& inorder, int in_left,int in_right,
vector<int>& postorder, int post_left, int post_right){
if(in_right < in_left || post_right < post_left) return NULL;
TreeNode *root = new TreeNode(postorder[post_right]);
int index = in_left;
for( ; index <= in_right; ++ index ) if(inorder[index] == postorder[post_right]) break;
int right_cnt = in_right-index;
root->left = buildTree(inorder,in_left,index-,postorder,post_left,post_right-right_cnt-);
root->right = buildTree(inorder,index+,in_right,postorder, post_right-right_cnt,post_right-);
return root;
} TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
if(inorder.size() == ) return NULL;
else return buildTree(inorder,,inorder.size()-, postorder,,postorder.size()-);
}
};

Leetcode Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

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

  2. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  3. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  4. LeetCode——Construct Binary Tree from Inorder and Postorder Traversal

    Question Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may a ...

  5. [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...

  6. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  7. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  8. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

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

随机推荐

  1. 创建NetWorkDataset---Shapefile篇

    部分参照esri的官方例子,理解下各个参数,对照自己的NetWorkDatase创建方式(在arcmap中),多试试代码就调好了. /// <summary> /// 创建NetWorkD ...

  2. todoList使用教程

    网页链接:http://www.cnblogs.com/sunada2005/articles/2663030.html

  3. Sql Server自动备份数据库,定期删除备份

    //实现:每天自动备份数据库,定期删除备份 //步骤:[开始]--[所有程序]--[Microsoft SQL Server 2005]--[SQL Server Management Studio] ...

  4. [Centos] Centos 7笔记

    命令行与图形界面 在X-Window图形操作界面中按“Alt+Ctrl+功能键Fnn=1~6”就可以进入Console字符操作界面.这就意味着你可以同时拥有X-Window加上6个Console字 ...

  5. PHP变量作用域(花括号、global、闭包)

    花括号 很多语言都以花括号作为作用域界限,PHP中只有函数的花括号才构成新的作用域. <?php if (True) { $a = 'var a'; } var_dump($a); for ($ ...

  6. Linux C fcntl()函数详解

    fcntl系统调用 功能描述:根据文件描述词来操作文件的特性. 用法: int fcntl(int fd, int cmd);  int fcntl(int fd, int cmd, long arg ...

  7. 7.1WebApi2的异常处理

    这篇文章描述错误和异常处理在 ASP.NET Web API. HttpResponseException 如果 Web API 控制器引发未捕获的异常,会发生什么?默认情况下,大多数异常被转译为 H ...

  8. 关于CDN的认识

    传统的未加缓存服务的访问过程: 用户提交域名→浏览器对域名进行解释→得到目的主机的IP地址→根据IP地址访问发出请求→得到请求数据并回复 由上可见,用户访问未使用CDN缓存网站的过程为: 1).用户向 ...

  9. jq小插件--方便设置css属性

    $.fn.extend({ setCss:function(name,value){ $(this).css(name,value) } }) 调用方式,如: $('.login-btn').setC ...

  10. 【Python基础学习二】定义变量、判断、循环、函数基本语法

    先来一个愉快的Hello World吧,就是这么简单,不需要写标点符号,但是需要严格按照缩进关系,Python变量的作用域是靠tab来控制的. print("Hello World" ...