【Leetcode】【Medium】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.
解题思路:
给出一个二叉树的中序和后序遍历结果,还原这个二叉树。
对于一个二叉树:
1
/ \
2 3
/ \ / \
4 5 6 7
后序遍历结果为:4 5 2 6 7 3 1
中序遍历结果为:4 2 5 1 6 3 7
由此可以发现规律:
1、后序遍历的最后一个字符,就是根结点(1)
2、发现根节点后,对应在中序遍历中的位置,则在中序遍历队列中,根节点左边的元素构成根的左子树,根的右边元素构成根的右子树;
3、递归的将左右子树也按照上述规律进行构造,最终还原二叉树。
代码:
/**
* Definition for a binary tree node.
* 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 buildSubtree(postorder, inorder, ,
postorder.size()-,
, inorder.size()-);
} TreeNode* buildSubtree(vector<int>& postorder, vector<int>& inorder,
int p_left, int p_right, int i_left, int i_right) {
if (p_left > p_right || i_left > i_right)
return NULL; int root = postorder[p_right];
TreeNode* node = new TreeNode(postorder[p_right]); int range = ;
for (int j = i_left; j <= i_right; ++j) {
if (root == inorder[j]) {
range = j - i_left;
break;
}
} node->left = buildSubtree(postorder, inorder,
p_left, p_left + range - ,
i_left, i_left + range - );
node->right = buildSubtree(postorder, inorder,
p_left + range, p_right - ,
i_left + range + , i_right);
return node;
}
};
【Leetcode】【Medium】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 Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 【题解二连发】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 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 ...
- LeetCode: 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: ...
- 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 ...
- 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 ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- VR内容是如何制作的!
VR全景视频作为一种新型的视频方式,其震撼效果是毋庸置疑的.目前市场上的VR全景视频也不在少数,越来越多的人能够欣赏到精彩的内容. 首先呢, VR内容场景的呈现分为两种情况: 1.实景拍摄 2.3D建 ...
- linux下VI模式中上下左右键和回退键出现字母
1.编辑/etc/vim/vimrc.tiny 由于/etc/vim/vimrc.tiny的拥有者是root用户,所以要在root的权限下对这个文件进行修改.很简单,这个文件里面的倒数第二句话是“se ...
- sencha touch SortableList 的使用
转: sencha touch 2.3 多了一个 SortableList plugin, 可实现 list item 的拖动交换 Ext.require('Ext.plugin.SortableL ...
- oracle timestamp转换date及date类型相减
--timestamp转换为date(ts字段为timestamp类型) ; --timestamp转换为date(ts字段为timestamp类型) ; --date相减 )) FROM dual; ...
- JSP中实现网页访问统计的方法【转】
我采用的是jsp网页,但是不管采用什么语言,原理是一样的. 第一种,单页面统计.就是说,只要点击这个页面就会统计一次. <body> <%!//在这种标记中定义的变量为全局变量 in ...
- Linux中常用头文件的作用--转
http://blog.sina.com.cn/s/blog_5c93b2ab0100q62k.html 1. Linux中一些头文件的作用: <assert.h>:ANSI C.提供断言 ...
- pyhon-爬虫实战抓取豆瓣top250到mysql
采集地址https://movie.douban.com/top250 一.创建mysql数据库 CREATE TABLE `t_doubantop` ( `id` int(11) unsigned ...
- show_space1
CREATE OR REPLACE PROCEDURE show_space1(p_segname IN VARCHAR2, p_owner IN VARCHAR2 DEFAULT 'NTSUSER0 ...
- T-SQL 备份和还原数据库
--完整备份 Backup Database db_database To disk='D:\Backup\db_database_Full.bak' --差异备份 Backup ...
- C#实现Javascript的Splice方法
最近开始学习Javascript语言,看到splice方法,以下引用其说明:该方法是一个通用删除和插入元素的方法,它可以在数组指定的位置开始删除或插入元素.其包括3个参数:第一个参数指定插入的起始位置 ...