前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 105: 从前序与中序遍历序列构造二叉树

LeetCode 105: 从前序与中序遍历序列构造二叉树

作者头像
爱写bug
发布2020-02-26 11:17:00
3560
发布2020-02-26 11:17:00
举报
文章被收录于专栏:爱写Bug爱写Bug

题目:

根据一棵树的前序遍历与中序遍历构造二叉树。

Given preorder and inorder traversal of a tree, construct the binary tree.

注意: 你可以假设树中没有重复的元素。

Note: You may assume that duplicates do not exist in the tree.

例如,给出

代码语言:javascript
复制
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]

返回如下的二叉树:

代码语言:javascript
复制
    3
   / \
  9  20
    /  \
   15   7

解题思路:

由两种遍历结果还原二叉树,是一种很经典的面试题型。其中中序遍历结果必须为已知才能还原二叉树。

本题为前序中序还原二叉树,回顾一下遍历顺序:

  • 前序遍历:根结点 -> 左子结点 -> 右子结点
  • 中序遍历:左子结点 -> 根结点 -> 右子结点
  • 后序遍历:右子结点 -> 左子结点 -> 根结点

看中序遍历,找到根结点,一定可以分成左右子树。例如二叉树:

代码语言:javascript
复制
    3
   / \
  9  20
    /  \
   15   7
代码语言:javascript
复制
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]

前序遍历第一个结点 3 ,就是根结点,找到中序遍历中结点值为 3 的位置:

代码语言:javascript
复制
中序遍历 inorder = [9,3,15,20,7]
                     ^
以 3 为根结点划分两个左右子树:
    3
   / \
[9]  [15,20,7]

前序遍历第二个结点 9,就是下一个子树的根结点,找到中序遍历中结点值为 9 的位置:

代码语言:javascript
复制
中序遍历 inorder :
    3
   / \
[9]  [15,20,7]
 ^

结点 9 为叶子结点,不可再划分:
    3
   / \
  9 [15,20,7]

前序遍历第三个结点 20,就是下一个子树的根结点,找到中序遍历中结点值为 20 的位置:

代码语言:javascript
复制
中序遍历 inorder :
    3
   / \
  9 [15,20,7]
         ^
以 20 为根结点划分两个左右子树:
    3
   / \
  9  20
    /  \
  [15] [7]

前序遍历第三个结点 15,就是下一个子树的根结点,找到中序遍历中结点值为 15 的位置:

……

由此可知中序遍历必须为已知条件才可找出出每个子树的根结点。

递归解法:

Java:

代码语言:javascript
复制
class Solution {
    int[] preorder;
    int[] inorder;
    int index_pre = 0;
    HashMap<Integer, Integer> index_Map = new HashMap<>();

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        this.preorder = preorder;
        this.inorder = inorder;
        // 将中序遍历结果的结点值与索引位置映射
        for (int i = 0; i < inorder.length; i++)
            index_Map.put(inorder[i], i);
        return helper(0, inorder.length - 1);
    }

    private TreeNode helper(int left, int right) {
        // 基线条件
        if (left > right)
            return null;
        // 按顺序逐个取前序遍历结果,并构造为根结点
        int root_val = preorder[index_pre++];
        TreeNode root = new TreeNode(root_val);
        // 查找该根结点在中序遍历中的索引位置
        int index_in = index_Map.get(root_val);
        // 构造递归
        root.left = helper(left, index_in - 1);
        root.right = helper(index_in + 1, right);
        return root;
    }
}

Python:

Python 中也可以将 index_pre 提升到全局变量,这里为了结构上的整齐,使用的是 nonlocal 关键字。

代码语言:javascript
复制
class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
        def helper(left=0, right=len(inorder)-1):
            nonlocal index_pre
            # 基线条件
            if left > right:
                return None
            # 按顺序逐个取前序遍历结果,并构造为根结点
            root_val = preorder[index_pre]
            root = TreeNode(root_val)
            # 查找该根结点在中序遍历中的索引位置
            index_in = index_map[root_val]
            # 构造递归
            index_pre += 1
            root.left = helper(left, index_in-1)
            root.right = helper(index_in+1, right)
            return root

        index_pre = 0
        # 将中序遍历结果的结点值与索引位置映射
        index_map = {val: index for index, val in enumerate(inorder)}
        return helper()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-02-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 爱写Bug 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目:
  • 解题思路:
  • 递归解法:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档