Middle-题目68:106. Construct Binary Tree from Inorder and Postorder Traversal

原创 2016年05月31日 16:38:31

题目原文:
Given inorder and postorder traversal of a tree, construct the binary tree.
题目大意:
给出一棵二叉树中序和后序遍历的序列,构建这个二叉树。
题目分析:
中序遍历的顺序是左-中-右,而后序遍历是左-右-中,所以取后序遍历的最后一个元素到中序遍历串中匹配,匹配到之后递归根据子树的中序和后序序列构建左右子树。
源码:(language:c)

struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize) {
    if(inorderSize == 0 && postorderSize == 0)
        return NULL;
    else {
        int middle = postorder[postorderSize-1];
        int i;
        for (i = 0;inorder[i] != middle; i++);
        struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); //generate root
        root->val = middle;
        root->left = buildTree(inorder, i, postorder, i);
        root->right = buildTree(&inorder[i+1], inorderSize-i-1, &postorder[i], inorderSize-i-1);
        return root;
    }
}

成绩:
20ms,35.14%,众数20ms,43.24%
Cmershen的碎碎念:
本题的Java入口参数是(int[],int[]),而C中使用的是指针,这样可以有效的截取子数组。所以指针虽然很容易引发各种缺陷,是危险的,但有时也提供了很大的方便。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)】

【106-Construct Binary Tree from Inorder and Postorder Traversal(通过中序和后序遍历构造二叉树II)】【LeetCode-面试算法经典-J...

Construct Binary Tree from Inorder and Postorder Traversal - LeetCode 106

题目描述: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may a...
  • bu_min
  • bu_min
  • 2015年05月27日 10:41
  • 237

leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal-递归

原题链接:106. Construct Binary Tree from Inorder and Postorder Traversal 【思路】 本题考查根据中序遍历和后序遍历构造二叉树。其基本思路...

leetcode题解-105 && 106. Construct Binary Tree from Preorder (PostOrder) and Inorder Traversal

其实这两道题目就是从二叉树的前序遍历(后序遍历)和中序遍历数组中恢复出二叉数即可。我们知道二叉树的三种遍历方法后本题就变得很简单了。 先序遍历: 若二叉树为空,则不进行任何操作...

【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 t...

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

LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers...

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

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树

1 解题思想这道题和105的思想,基本就是一致的,这里将会说的比较简单,之说下不同 Leetcode 105. Construct Binary Tree from Preorder and Ino...
  • MebiuW
  • MebiuW
  • 2016年09月16日 16:42
  • 313

LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal

LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder TraversalDescriptionGiven inorder ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目68:106. Construct Binary Tree from Inorder and Postorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)