LeetCode(算法)- 105. 从前序与中序遍历序列构造二叉树

Posted 放羊的牧码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(算法)- 105. 从前序与中序遍历序列构造二叉树相关的知识,希望对你有一定的参考价值。

题目链接:点击打开链接

题目大意:

解题思路:

相关企业

  • 字节跳动
  • Facebook
  • 亚马逊(Amazon)
  • 谷歌(Google)
  • 微软(Microsoft)
  • 优步(Uber)
  • 彭博(Bloomberg)

AC 代码

  • Java
/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */

// 解决方案(1)
class Solution 

    int[] preorder, inorder;

    Map<Integer, Integer> inorderMap = new HashMap<>();

    public TreeNode buildTree(int[] preorder, int[] inorder) 
        this.preorder = preorder;
        this.inorder = inorder;
        for (int i = 0; i < inorder.length; i++) 
            inorderMap.put(inorder[i], i);
        
        return recur(0, inorder.length - 1, 0, preorder.length - 1);
    

    TreeNode recur(int la, int ra, int lb, int rb) 
        if (la > ra) return null;
        int preRoot = preorder[lb];
        int midRoot = inorderMap.get(preRoot);
        int leftLen = midRoot - la;
        TreeNode root = new TreeNode(preRoot);
        root.left = recur(la, midRoot - 1, lb + 1, lb + leftLen);
        root.right = recur(midRoot + 1, ra, lb + leftLen + 1, rb);

        return root;
    


// 解决方案(2)
class Solution 
    int[] preorder;
    HashMap<Integer, Integer> dic = new HashMap<>();
    public TreeNode buildTree(int[] preorder, int[] inorder) 
        this.preorder = preorder;
        for(int i = 0; i < inorder.length; i++)
            dic.put(inorder[i], i);
        return recur(0, 0, inorder.length - 1);
    
    TreeNode recur(int root, int left, int right) 
        if(left > right) return null;                          // 递归终止
        TreeNode node = new TreeNode(preorder[root]);          // 建立根节点
        int i = dic.get(preorder[root]);                       // 划分根节点、左子树、右子树
        node.left = recur(root + 1, left, i - 1);              // 开启左子树递归
        node.right = recur(root + i - left + 1, i + 1, right); // 开启右子树递归, root + i - left + 1 => root + (left - (i - 1) + 1) + 1 (根节点索引 + 左子树长度 + 1)
        return node;                                           // 回溯返回根节点
    
  • C++
/**
 * Definition for a binary tree node.
 * struct TreeNode 
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) 
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) 
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) 
 * ;
 */
class Solution 
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) 
        this->preorder = preorder;
        for(int i = 0; i < inorder.size(); i++)
            dic[inorder[i]] = i;
        return recur(0, 0, inorder.size() - 1);
    
private:
    vector<int> preorder;
    unordered_map<int, int> dic;
    TreeNode* recur(int root, int left, int right)  
        if(left > right) return nullptr;                        // 递归终止
        TreeNode* node = new TreeNode(preorder[root]);          // 建立根节点
        int i = dic[preorder[root]];                            // 划分根节点、左子树、右子树
        node->left = recur(root + 1, left, i - 1);              // 开启左子树递归
        node->right = recur(root + i - left + 1, i + 1, right); // 开启右子树递归, root + i - left + 1 => root + (left - (i - 1) + 1) + 1 (根节点索引 + 左子树长度 + 1)
        return node;                                            // 回溯返回根节点
    
;

以上是关于LeetCode(算法)- 105. 从前序与中序遍历序列构造二叉树的主要内容,如果未能解决你的问题,请参考以下文章

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

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

Leetcode 105. 从前序与中序遍历序列构造二叉树

LeetCode105. 从前序与中序遍历序列构造二叉树

[JavaScript 刷题] 树 - 从前序与中序遍历序列构造二叉树, leetcode 105

LeetCode第105题—从前序与中序遍历序列构造二叉树—Python实现