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.

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

大体要求就是给一个中序遍历的数组和一个后序遍历的数组,构造一个二叉树。

一.我的解法

大致的思路跟之前105的一样,只不过因为后序遍历,所以根节点都处在数组的末尾处。时间复杂度是O(n),空间复杂度是O(n)。

class Solution {
    Map<Integer,Integer> map=new HashMap<>();
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        for(int i=0;i<inorder.length;i++){
            map.put(inorder[i],i);
        }
        return construct(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
    }
    public TreeNode construct(int[] inorder,int instart,int inend,int[] postorder,int posstart,int posend){
        if(instart>inend||posstart>posend) return null;
        TreeNode root=new TreeNode(postorder[posend]);
        int posRoot=map.get(root.val);

        int leftlen=posRoot-instart;
        root.left=construct(inorder,instart,posRoot-1,postorder,posstart,leftlen+posstart-1);
        root.right=construct(inorder,posRoot+1,inend,postorder,leftlen+posstart,posend-1);
        return root;
    }
}

二.拓展

其实之前在105时就在想除了递归的解法外,有没有迭代的替代解法。然后,就在discussion板块发现了其中一种迭代的解法,而且发现这个思路很赞。所以就按照这个思路又写了一种算法。这个算法的核心思想就是:

首先从后序和中序数组的末尾扫起,因为后序的末尾是根节点,且从后往前直达最右边的结点为止,下标为n-1 的结点一定是下标为n 的结点的右子树,将经过的结点压入栈中,而且这时的结点值一定与中序的末尾不一样,因为中序的数组末尾一定是最右结点。然后只要检测到后序数组的最右结点时,便可以一直返回到还有左子树的结点,再之后就一直重复以上操作。

时间复杂度的话,对于一般情况下的平衡二叉树,是O(nlogn),对于斜树好像是在O(n)。

这个是参考的原地址:
https://discuss.leetcode.com/topic/8207/java-iterative-solution-with-explanation

class Solution{
    public TreeNode buildTree(int[] inorder,int[] postorder){
        if(inorder.length==0||postorder.length==0) return null;
        Stack<TreeNode> stack=new Stack<>();
        int pend=postorder.length-1,iend=inorder.length-1;
        TreeNode root=new TreeNode(postorder[pend--]);
        stack.push(root);
        while(pend>=0){
            TreeNode cur=stack.peek();
            if(cur.val!=inorder[iend]){
                TreeNode right=new TreeNode(postorder[pend--]);
                cur.right=right;
                stack.push(right);
            }
            else{
                while(!stack.isEmpty()&&stack.peek().val==inorder[iend]){
                    cur=stack.pop();
                    iend--;
                }
                TreeNode left=new TreeNode(postorder[pend--]);
                cur.left=left;
                stack.push(left);
            }
        }
        return root;

    }
}

如果大家有什么意见或者我有什么错误的话,请指出来,我会积极改进。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值