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. Linux服务器集群技术的概述

    目前,越来越多的网站采用Linux操作系统,提供邮件.Web.文件存储.数据库等服务.也有非常多的公司在企业内部网中利用Linux服务器提供这些服务.随着人们对Linux服务器依赖的加深,对其可靠性. ...

  2. jQuery Ajax 分页插件

    很多社交网站都使用无限滚动的翻页技术来提高用户体验,当你页面滑到列表底部时候无需点击就自动加载更多的内容 很多社交网站都使用无限滚动的翻页技术来提高用户体验,当你页面滑到列表底部时候无需点击就自动加载 ...

  3. 只要是从事IT,会些CSS,XHTML总归是有好处的

    上次是十多年前看了的,这次又系统看了下.. 这系统的HEAD FIRST,我很喜欢...收藏过三四本啦...

  4. HDU4681 String(dp)

    题目链接. #include <iostream> #include <cstdio> #include <cstring> #include <cstdli ...

  5. Linux kernel ‘key_notify_policy_flush’函数信息泄露漏洞

    漏洞名称: Linux kernel ‘key_notify_policy_flush’函数信息泄露漏洞 CNNVD编号: CNNVD-201307-072 发布时间: 2013-07-05 更新时间 ...

  6. Light OJ 1018 - Brush (IV)

    题目大意:     一个二维平面上有N个点,一把刷子,刷一次可以把一条线上的所有点都刷掉.问最少刷多少次,可以把全部的点都刷完 状态压缩DP, 用记忆化搜索来写, 需要有个优化不然会超时. ===== ...

  7. ACM生活总结

    两年ACM生活总结 转眼已经踏入ACM这条不归路已经两年了, 深深的感觉到ACM的不易 和 艰辛,但同时ACM给我所带来的快乐,让我认为值一切都是值得的. 我刚上大学那会,我们学校的ACM刚刚起步不到 ...

  8. 【Android Studio】No JVM installation found

    如果没有配置好JDK的环境变量,启动Android Studio的时候会报错: 请参考我整理的博客文章<JDK的下载.安装和配置>,链接:http://www.cnblogs.com/du ...

  9. Servlet工作原理

    Servlet生命周期分为三个阶段: 1,初始化阶段  调用init()方法 2,响应客户请求阶段 调用service()方法 3,终止阶段 调用destroy()方法 Servlet初始化阶段: 在 ...

  10. servlet简介

    web 开发分为两种:静态开发(使用html)和动态开发(使用servlet/jsp,jsp就是servlet,ASP ,PHP) 所以servlet是sun公司提供的一门专门用于开发动态web资源的 ...