leetcode 刷题之路 64 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.
给出二叉树的中序遍历和后序遍历结果,恢复出二叉树。
后序遍历序列的最后一个元素值是二叉树的根节点的值。查找该元素在中序遍历序列中的位置mid,依据中序遍历和后序遍历性质。有:
位置mid曾经的序列部分为二叉树根节点左子树中序遍历的结果,得出该序列的长度n,则后序遍历序列前n个元素为二叉树根节点左子树后序遍历的结果。由这两个中序遍历和后序遍历子序列恢复出左子树。
位置mid以后的序列部分为二叉树根节点右子树中序遍历的结果,得出该序列的长度m,则后序遍历序列(除去最后一个元素)后m个元素为二叉树根节点右子树后序遍历的结果,由这两个中序遍历和后序遍历子序列恢复出左子树;
以上描写叙述中递归地引用了由中序遍历和后序遍历恢复子树的部分,因此程序也採用递归实现。
AC code:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ TreeNode *helper(vector<int> &inorder, int b1, int e1, vector<int> &postorder, int b2, int e2)
{
if (b1>e1)
return NULL;
int mid;
for (int i = b1; i <= e1; i++)
if (inorder[i] == postorder[e2])
{
mid = i;
break;
} TreeNode* root = new TreeNode(inorder[mid]);
root->left = helper(inorder, b1, mid - 1, postorder, b2, b2 + mid - b1-1);
root->right = helper(inorder, mid + 1, e1, postorder, b2 + mid-b1, e2 - 1);
return root;
}
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
return helper(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);
}
};
leetcode 刷题之路 64 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: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 ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 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: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- 【题解二连发】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 ...
- 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 ...
随机推荐
- 查看Linux下网卡状态或 是否连接
分类: 1) 通过mii-tool指令 [root@localhost root]# mii-tool eth0: negotiated 100baseTx-FD, link ...
- 对TCP说三道四(三次握手)
夜朦胧,人方静,无聊的人打开了无聊的电脑看到了一张无聊的图,想着想着就睡着了,梦到了人a和人b的一次聊天. 有一天,a有事情想跟b商量就问b“有时间么,想和你聊一下天”,b想了一会发现自己能抽出时间就 ...
- Final Exam Arrangement(ZOJ)
In Zhejiang University, there are N different courses labeled from 1 to N. Each course has its own t ...
- Hibernate 、多表关联映射-组件关联映射(component)
组件关联映射可以将一些简小的数据与主题放在一个表中,例如firstName 和LastName这两个结合在一起可以组成一个名字,但是再分别将这两个再建一个表就不太合适了,这个时候可以用到组件关联映射: ...
- Cipher(置换群)
Cipher Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20821 Accepted: 5708 Descripti ...
- [置顶] Mysql存储过程入门知识
Mysql存储过程入门知识 #1,查看数据库所有的存储过程名 #--这个语句被用来移除一个存储程序.不能在一个存储过程中删除另一个存储过程,只能调用另一个存储过程 #SELECT NAME FROM ...
- 使用VNC完毕远程调用图形化
原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明下面出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...
- 排颜色问题——数组 leetcode lintcode
问题描写叙述: 给一个数组,而且数组里面元素的值仅仅可能是0,1,2,然后如今把这个数组排序. 第二种表述: 现有n个红白蓝三种不同颜色的小球,乱序排列在一起,请通过两两交换随意两个球,使得从左至右, ...
- Python中文字符的理解:str()、repr()、print
Python中文字符的理解:str().repr().print 字数1384 阅读4 评论0 喜欢0 都说Python人不把文字编码这块从头到尾.从古至今全研究通透的话是完全玩不转的.我终于深刻的理 ...
- WPF子界面向父界面传递带参数的委托
需求如下: 父界面上有几个按钮,需要在点击按钮的时候向父界面传值,以知道该控制哪个界面显示. 父界面:WIN1 子界面:WIN2 此处或者说是子控件向父控件传值 1.子界面定义委托 WIN2里写: p ...