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. CodeChef FNCS

    题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...

  2. var a =a || {}

  3. windows phone 8学习 - 选择器

    1照相机 CameraCaptureTask cct=new CameraCaptureTask(); cct.Completed+=new EventHandler<PhotoResult&g ...

  4. 【转】JQUERY相关的几个网站

    作者:Terry li - GBin1.com 1. John Resig - http://ejohn.org 毫无疑问,jQuery 的缔造者的博客是你首先必须关注的. 2. Filmament ...

  5. BZOJ 1009 GT考试

    Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学A1A2...Am(0< ...

  6. BZOJ 1022 小约翰的游戏

    Description 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取 ...

  7. C 和 OC 字符串转换 NSString 和 char * 转换 const char* 与 char *

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { char *s = "He ...

  8. linux下so动态库一些不为人知的秘密(转)

    linux 下有动态库和静态库,动态库以.so为扩展名,静态库以.a为扩展名.二者都使用广泛.本文主要讲动态库方面知识.基本上每一个linux 程序都至少会有一个动态库,查看某个程序使用了那些动态库, ...

  9. hdu 4289 最小割,分拆点为边

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2609 #include <cstdio> #incl ...

  10. 阿里巴巴算法工程师四面(三轮技术+hr面)详细面经

    阿里面试总结: 一遍一遍地刷阿里网站,今天发现“面试中”变成“待跟进offer”了,写个面经攒人品,希望offer通知邮件早点来吧. 我当时投简历时投了C/C++工程师,其实也没经过啥考虑,因为我一开 ...