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 duplicates do not exist in the tree.
这个题目是给你一棵树的中序遍历和后序遍历,让你将这棵树表示出来。其中可以假设在树中没有重复的元素。
当做完这个题之后,建议去做做第105题,跟这道题类似。
分析:这个解法的基本思想是:我们有两个数组,分别是IN和POST.后序遍历暗示POSR[end](也就是POST数组的最后一个元素)是根节点。之后我们可以在IN中寻找POST[END].假设我们找到了IN[5].现在我们就能够知道IN[5]是根节点,然后IN[0]到IN[4]是左子树,IN[6]到最后是右子树。然后我们可以通过递归的方式处理这个数组。
代码如下,其中改代码击败了100%的C#提交者:
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode BuildTree(int[] inorder, int[] postorder) {
return binaryTree(postorder.Length-,,inorder.Length-,inorder,postorder);
} public TreeNode binaryTree(int postEnd,int inStart,int inEnd,int[] inorder, int[] postorder)
{
if(postEnd<||inStart>inEnd)
return null; int inindex=;
TreeNode root=new TreeNode(postorder[postEnd]); for(int i=inStart;i<=inEnd;i++)
{
if(inorder[i]==postorder[postEnd])
{
inindex=i;
break;
}
} root.left=binaryTree(postEnd-(inEnd-inindex)-,inStart,inindex-,inorder,postorder);
root.right=binaryTree(postEnd-,inindex+,inEnd,inorder,postorder); return root; }
}
最最关键的是确定函数的实参的时候,一定不能弄错。
root.left=binaryTree(postEnd-(inEnd-inindex)-1,inStart,inindex-1,inorder,postorder);
中的inEnd-inindex代表了右子树的元素数,为了求得左子树的最后一位,应该将该元素减去。
C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- 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 ...
- 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 由中序和后序遍历建立二叉树 C++
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 ...
随机推荐
- Java线程生命模型
一. 线程状态类型1. 新建状态(New):新创建了一个线程对象.2. 就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运 ...
- 试玩GitHub
SVN是可以,但GitHub越来越屌啊... SO...要玩起来.. 参考URL: http://jingyan.baidu.com/article/f7ff0bfc7181492e27bb1360. ...
- VSTO:无法安装此应用程序,因为已安装具有相同标识的应用程序
原文:VSTO:无法安装此应用程序,因为已安装具有相同标识的应用程序 在开发环境(Win7+VS2010+Outlook 2010)上做一个Outlook AddIn,运行Outlook时报如下错误: ...
- 对于唯一索引使用唯一条件搜索, InnoDB 只锁定找到的index record,不是它之前的区间
| test100 | CREATE TABLE `test100` ( `sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增编号', `phoneNo` ...
- java学习之数组(二)
在上一节中我们讲到了数组的概念,定义,以及在内存当中的表现形式.那么这里我们来说一下,数组的另一种定义方式. 在上一篇当中我们规定是这个样子定义数组的, class ArrDemo { public ...
- centos6.5安装gcc6.1等c++环境
centos6.5安装gcc6.1等c++环境 1.获取gcc安装包并解压wget http://ftp.gnu.org/gnu/gcc/gcc-6.1.0/gcc-6.1.0.tar.bz2tar ...
- Cut the Cake(大数相乘)
MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly o ...
- ZOJ-3597-Hit the Target!(线段树+扫描线)
题解引自:http://www.cnblogs.com/wuyiqi/archive/2012/04/28/2474614.html 这题和着题解一块看,看了半天才看懂的....菜菜.... 题意:有 ...
- 3 B. Lorry
题目大意:给你两种物品,每种物品有一个价值和花费,花费只有两种,一种花费为 , 一种花费为2.. 给你一个背包容量为v, 求当前容量下所能达到的最大价值. ====================== ...
- 数据结构(线段树):CodeForces 145E Lucky Queries
E. Lucky Queries time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...