【一次过】Lintcode 72. 中序遍历和后序遍历树构造二叉树

本文介绍了一种利用中序遍历和后序遍历数组构建二叉树的方法,通过递归方式确定根节点,并划分左右子树,最终还原整个二叉树结构。

根据中序遍历和后序遍历树构造二叉树

样例

给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2]

返回如下的树:

  2

 /  \

1    3

注意事项

你可以假设树中不存在相同数值的节点


解题思路:

类似于Lintcode 73. 前序遍历和中序遍历树构造二叉树,直接看代码

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param inorder: A list of integers that inorder traversal of a tree
     * @param postorder: A list of integers that postorder traversal of a tree
     * @return: Root of a tree
     */
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        // write your code here
        Map<Integer,Integer> map= new HashMap<>();//key为inorder数组中的值,value为对应的数组索引
        for(int i=0 ; i<inorder.length ; i++)
            map.put(inorder[i],i);
        
        return buildTree(inorder,0,inorder.length-1,postorder,0,postorder.length-1,map);
    }
    
    public TreeNode buildTree(int[] inorder, int inStart, int inEnd, int[] postorder, int postStart, int postEnd,Map<Integer,Integer> map){
        if(inStart>inEnd || postStart>postEnd)
            return null;
        
        int rootVal = postorder[postEnd];
        int rootIndex = map.get(rootVal);
        
        int len = rootIndex - inStart;
        TreeNode root = new TreeNode(rootVal);
        root.left = buildTree(inorder,inStart,rootIndex-1,postorder,postStart,postStart+len-1,map);
        root.right = buildTree(inorder,rootIndex+1,inEnd,postorder,postStart+len,postEnd-1,map);
        
        return root;
    }
}

方法二:

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param inorder: A list of integers that inorder traversal of a tree
     * @param postorder: A list of integers that postorder traversal of a tree
     * @return: Root of a tree
     */
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        // write your code here
        return buildTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1);
    }
    
    private TreeNode buildTree(int[] inorder, int inStart, int inEnd, int[] postorder, int postStart, int postEnd){
        if(inStart > inEnd || postStart > postEnd)
            return null;
        
        int rootVal = postorder[postEnd];
        TreeNode root = new TreeNode(rootVal);
        
        int rootIndex = 0;
        for(int i=inStart ; i<=inEnd ; i++){
            if(inorder[i] == rootVal)
                rootIndex = i;
        }
        int len = rootIndex - inStart;
        
        root.left = buildTree(inorder, inStart, rootIndex-1, postorder, postStart, postStart+len-1);
        root.right = buildTree(inorder, rootIndex+1, inEnd, postorder, postStart+len, postEnd-1);
        
        return root;
    }
}

 

能将遍历结果还原为唯一二叉树遍历列组合有以下两种: ### 中序遍历后序遍历 后序遍历的最后一个元素是二叉树的根节点。在中序遍历中找到根节点的位置,根节点左边的列是左子的中序遍历,右边的列是右子的中序遍历。在后序遍历中找到对应的左子右子列,左子的长度序遍历中左子的长度相同,右子也一样。递归地对左子右子重复上述步骤,直到后序遍历或中序遍历为空[^2]。 以下是使用Python实现根据中序遍历后序遍历还原二叉树的示例代码: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def buildTree(inorder, postorder): if not inorder or not postorder: return None root_val = postorder[-1] root = TreeNode(root_val) inorder_index = inorder.index(root_val) root.left = buildTree(inorder[:inorder_index], postorder[:inorder_index]) root.right = buildTree(inorder[inorder_index + 1:], postorder[inorder_index:-1]) return root ``` ### 中序遍历序遍历序遍历的第一个元素是二叉树的根节点。在中序遍历中找到根节点的位置,根节点左边的列是左子的中序遍历,右边的列是右子的中序遍历。根据左子右子序遍历的长度,在先序遍历中划分出左子右子的先序遍历列。递归地对左子右子重复上述步骤,直到先序遍历或中序遍历为空。 以下是使用Python实现根据中序遍历序遍历还原二叉树的示例代码: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def buildTree(preorder, inorder): if not preorder or not inorder: return None root_val = preorder[0] root = TreeNode(root_val) inorder_index = inorder.index(root_val) root.left = buildTree(preorder[1:inorder_index + 1], inorder[:inorder_index]) root.right = buildTree(preorder[inorder_index + 1:], inorder[inorder_index + 1:]) return root ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值