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.

For example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Return the following binary tree:

    3
   /   9  20
    /     15   7

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

距离太远就要相加。相同的题还是一起做比较好,隔一段时间再去理解 实在太心累了。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

int idx = map.get(posorder[posStart]); 从postorder中取出index作为后续使用才行

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 比较远时,加上中-右 = inidx - inend

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

都怪recursive不好跑case,算了,距离太远就要相加。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {

    public TreeNode buildTree(int[] inorder, int[] posorder) {
        //corner case
        if (inorder == null || posorder == null || posorder.length != inorder.length) return null;

        //initialization: put (posorder[i], i) into map
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < inorder.length; i++)
            map.put(inorder[i] , i);

        //dfs and return
        return dfs(inorder, 0, inorder.length - 1, posorder, posorder.length - 1, 0, map);
    }

    public TreeNode dfs(int[] inorder, int inStart, int inEnd,
                    int[] posorder, int posStart, int posEnd,
                   HashMap<Integer, Integer> map) {
        //exit case
        if (inStart > inEnd || posStart > posEnd) return null;

        //find inIdx and do dfs
        TreeNode root = new TreeNode(posorder[posStart]);
        int inIdx = map.get(root.val);

        //do dfs in left and right and add to root
        root.left = dfs(inorder, inStart, inIdx - 1, posorder, posStart + (inIdx - inEnd) - 1, posEnd, map);
        root.right = dfs(inorder, inIdx + 1, inEnd, posorder, posStart- 1, posEnd, map);

        return root;
    }
}

原文地址:https://www.cnblogs.com/immiao0319/p/9527054.html

时间: 2024-10-14 11:40:15

106. Construct Binary Tree from Inorder and Postorder 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 that duplicates do not exist in the tree. 题目标签:Array, Tree 这到题目和105 几乎是一摸一样的,唯一的区别就是把pre-order 换成 post-order.因为post-order是最后一个数字是root,所以要从右向左的遍历.还需要把helpe

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

leetcode题解: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. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

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: 115870 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 解题思路

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. ======== 利用:中序+后序遍历 ==== code: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod

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

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. 思路:这题和上题类似,前序第一个是根节点,后序遍历最后一个是根节点.其余步骤类似. 代码如下: /** * Definition for a binary tree node. * public class TreeNod

106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {

【LeetCode】105 &amp; 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. 提示: 题目要求通过一颗二叉树的中序遍历及后续遍历的结果,将这颗二叉树构建出来,另外还有一个已知条件,所有节点的值都是不同的. 首先需要了解一下二叉树不同遍历方式的定义: 前序遍历:首先访问根结点,然后遍历左子树,最