【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
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 duplicates do not exist in the tree.
根据定义,后序遍历postorder的最后一个元素为根。
由于元素不重复,通过根可以讲中序遍历inorder划分为左子树和右子树。
递归下去即可求解。
- /**
- * Definition for binary 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) {
- return Helper(inorder, , inorder.size()-, postorder, , postorder.size()-);
- }
- TreeNode* Helper(vector<int>& inorder, int begin1, int end1, vector<int>& postorder, int begin2, int end2)
- {
- if(begin1 > end1)
- return NULL;
- else if(begin1 == end1)
- return new TreeNode(inorder[begin1]);
- TreeNode* root = new TreeNode(postorder[end2]);
- int i = begin1;
- for(; i <= end1; i ++)
- {
- if(inorder[i] == postorder[end2])
- break;
- }
- int leftlen = i-begin1;
- root->left = Helper(inorder, begin1, begin1+leftlen-, postorder, begin2, begin2+leftlen-);
- root->right = Helper(inorder, begin1+leftlen+, end1, postorder, begin2+leftlen, end2-);
- return root;
- }
- };
【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- 【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 Traversall
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- LeetCode OJ 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】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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】105 & 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 ...
- 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 Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- md5加密,md5加盐加密和解密
package com.java.test; import java.security.MessageDigest; import java.security.SecureRandom; import ...
- Markdown 简明语法手册 - 作业
目录 Cmd Markdown 简明语法手册 1. 内容目录 2. 标签分类 3. 删除线 水平线--- 1. 斜体和粗体 2. 分级标题 标题1 标题2 标题3 3. 外链接 4. 无序列表 5. ...
- FreeRTOS API
Task Creation xTaskCreate vTaskDelete Task Control vTaskDelay vTaskDelayUntil uxTaskPriorityGet vTas ...
- 执行计划解读 简朝阳 (Sky Jian) and 那蓝蓝海
http://greemranqq.iteye.com/blog/2072878 http://www.mysqlab.net/ http://www.mysqlpub.com/ http://blo ...
- IIS发布站点错误收集
转载:http://www.cnblogs.com/hangwei/p/4249406.html 本文主要收集IIS在发布站点过程中遇到的错误,并提供解决办法.并亲测可行.如果您也在使用IIS发布站点 ...
- TSL / SSL
参考: http://www.ruanyifeng.com/blog/2014/09/illustration-ssl.html http://www.tuicool.com/articles/IJ3 ...
- Ext Connection
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- cocos2d-x hello world及安卓平台迁移
本节和大家一起新建一个项目工程,并通过cygwin迁移至android平台. 以下是本节主要内容: 利用cocos2d-x自带脚本,生成测试工程,并测试运行: 将该测试项目通过cyg ...
- ORACLE单表理论最大记录数
不考虑硬件诸如内存,存储等硬件的限制. 一张表理论能存储多少条记录呢? 假设: 一个tablespace中包含1022个datafiles, 单个datafiles的最大是32G 假设每个block是 ...
- python接口自动化9-https请求(SSL)
前言 本来最新的requests库V2.13.0是支持https请求的,但是一般写脚本时候,我们会用抓包工具fiddler,这时候会报:requests.exceptions.SSLError: [S ...