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的更多相关文章

  1. 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: ...

  2. (二叉树 递归) 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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

    原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...

  7. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  8. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. while if 循环判断

    temp=input("猜一下我想的那个数字吧:") guess=int(temp) while guess!=8: temp=input("诶呀错误了在输入一次吧:&q ...

  2. iOS开源项目

    在结束了GitHub平台上“最受欢迎的Android开源项目”系列盘点之后,我们正式迎来了“GitHub上最受欢迎的iOS开源项目”系列盘点.今天,我们将介绍20个在GitHub上非常受开发者欢迎的i ...

  3. vmware中ubuntu更新内核后无法进入桌面,鼠标“漂移”滑动

    问题背景: 我机子上是在vmware下安装了ubuntu12.04,今天正在ubuntu下工作,结果提示内核有更新,手贱的就点了个OK,开始更新,更新完重启.结果,问题来了,刚开始系统启动,进入系统登 ...

  4. hadoop 异常 datanode未启动

    暴力方法:(本人是学习阶段,实际工作中不能这么做)在各个节点上执行如下操作. 将/tmp 删除 将 conf/mapred-site.xml <property> <name> ...

  5. iOS Developer Libray (中文版)-- Defining Classes 定义类

    该篇是我自己学习iOS开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更 ...

  6. Delphi编程中资源文件的应用

    Delphi编程中资源文件的应用/转自 http://chamlly.spaces.live.com/blog/cns!548f73d8734d3acb!236.entry一.引子: 现在的Windo ...

  7. 【HDOJ】2371 Decode the Strings

    快速矩阵乘法.注意,原始字符串即为decode后的字符串.题目是要找到原始串. #include <cstdio> #include <cstring> #define MAX ...

  8. Unity GUI编程

    脚本语言:C# 附上一张图说明Unity GUI编程中可用的控件:(可能有遗漏) 下面列出一些例子来说明: 1.Groups : 在固定Layout模式中起到组织可用项的功能,它让你在屏幕的一个区域中 ...

  9. jdbc.properties 包含多种数据库驱动链接的版本。

    # Properties file with JDBC-related settings. ########## # HSQLDB # ########## #jdbc.driverClassName ...

  10. 数据结构(虚树,动态规划):HNOI 2014 世界树

    Hnoi2014 世界树 Description 世界树是一棵无比巨大的树,它伸出的枝干构成了整个世界.在这里,生存着各种各样的种族和生灵,他们共同信奉着绝对公正公平的女神艾莉森,在他们的信条里,公平 ...