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 duplicates do not exist in the tree.
For example, given
inorder = [,,,,]
postorder = [,,,,]
Return the following binary tree:
/ \
/ \
中序、后序遍历得到二叉树,可以知道每一次新数组的最后一个数为当时子树的根节点,每次根据中序遍历的根节点的左右两边确定左右子树,再对应后序的左右子树,不停递归得到根节点,可以建立二叉树。每次由循环得到根节点在中序数组中坐标i
由中序遍历知:每一次inorder的左子树范围[ileft,i-1],右子树范围[i+1,iright]
由后序遍历知:每一次postorder的左子树范围[pleft,pleft+i-ileft-1],右子树范围[pleft+i-ileft,pright-1]。C++
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return buildTree(inorder,,inorder.size()-,postorder,,postorder.size()-);
}
TreeNode* buildTree(vector<int>& inorder,int ileft,int iright,vector<int>& postorder,int pleft,int pright){
if(ileft>iright||pleft>pright)
return NULL;
TreeNode* root=new TreeNode(postorder[pright]);
int i=;
for(i=ileft;i<=iright;i++){
if(inorder[i]==postorder[pright])
break;
}
root->left=buildTree(inorder,ileft,i-,postorder,pleft,pleft+i-ileft-);
root->right=buildTree(inorder,i+,iright,postorder,pleft+i-ileft,pright-);
return root;
}
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 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 ...
- 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 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 ...
- 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(medium)
原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...
- Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal
原题地址 二叉树基本操作 [ ]O[ ] [ ][ ]O 代码: TreeNode *restore(vector<i ...
- 【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 ...
随机推荐
- ms16-032漏洞复现过程
这章节写的是ms16-032漏洞,这个漏洞是16年发布的,版本对象是03.08和12.文章即自己的笔记嘛,既然学了就写下来.在写完这个漏洞后明天就该认真刷题针对16号的比赛了.Over,让我们开始吧! ...
- js·逻辑运算
|| 遇到第一个为真就返回 && 遇到第一个为假就终止,返回false,如果没遇到就返回最后那一个 5&&4&&2&&1 ==> ...
- python3+qqBot+图灵机器人实现qq聊天机器人
原理: 通过Python3的qqBot开源库,基于腾讯的smartQQ协议登录个人QQ,实现监控.收集QQ消息,进而通过图灵机器人API接入方式实现自动聊天. 零.前期准备: 1.Python3 2. ...
- Python基础:六、变量和常量
一.变量 1. 变量: 将运算的中间结果暂存到内存,以便后续程序调用 2. 变量的作用: 代指内存里某个地址中保存的内容 3. 变量的命名规则: 1. 变量由字母.数字.下划线搭配组合而成 2. 不可 ...
- Mvc Session 设置以后再构造函数中取值时为null问题
在登录界面写了一个session在 别的页面的构造函数中获取始终未null 后来改成 System.Web.HttpContext.Current.Session["User"] ...
- Excel 导入 Mysql
1.将Excel xls文件 另存为 csv 文件: 2.用记事本打开csv文件,然后另存为编码为utf-8的txt文件: 3.将txt文件后缀更改为csv 4.最后,用phpmyadmin或其他数据 ...
- Java高级特性 第8节 网络编程技术
一.网络概述 1.网络的概念和分类 计算机网络是通过传输介质.通信设施和网络通信协议,把分散在不同地点的计算机设备互连起来,实现资源共享和数据传输的系统.网络编程就就是编写程序使联网的两个(或多个)设 ...
- day01知识点
1.计算机基础 2.Python的历史 3.编码语言分类 Python是一门动态解释性的强制类型定义语言 4.Python的解释器种类 5.变量 法律规则:字母,数字,下划线(数字不能 ...
- Visual Studio Code Java输出中文乱码的问题
Visual Studio Code 推出了java插件,最近适用了一把,非常不错,但是有个很明显的bug.就是中文乱码,具体现象有如下: 1.System.out.println 控制台输出乱码. ...
- web.py模块使用
web.py模块 import time import web urls=("/",'hello') class hello(): def GET(self): return (t ...