Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【LeetCode每日打卡】105. Construct Binary Tree from Preorder and Inorder Traversal

【LeetCode每日打卡】105. Construct Binary Tree from Preorder and Inorder Traversal

作者头像
韩旭051
发布于 2022-05-09 00:57:54
发布于 2022-05-09 00:57:54
16800
代码可运行
举报
文章被收录于专栏:刷题笔记刷题笔记
运行总次数:0
代码可运行

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

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

For example, given

preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree:

    3    / \   9  20     /  \    15   7

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我太菜了,数据结构的基础题现在早忘了 刚好 复习一下

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    private Map<Integer, Integer> indexMap;

    public TreeNode myBuildTree(int[] preorder, int[] inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) {
        if (preorder_left > preorder_right) {
            return null;
        }
        // 前序遍历中的第一个节点就是根节点
        int preorder_root = preorder_left;
        // 在中序遍历中定位根节点
        int inorder_root = indexMap.get(preorder[preorder_root]);
        // 先把根节点建立出来
        TreeNode root = new TreeNode(preorder[preorder_root]);
        // 得到左子树中的节点数目
        int size_left_subtree = inorder_root - inorder_left;
        // 递归地构造左子树,并连接到根节点
        // 先序遍历中「从 左边界+1 开始的 size_left_subtree」个元素就对应了中序遍历中「从 左边界 开始到 根节点定位-1」的元素
        root.left = myBuildTree(preorder, inorder, preorder_left + 1, preorder_left + size_left_subtree, inorder_left, inorder_root - 1);
        // 递归地构造右子树,并连接到根节点
        // 先序遍历中「从 左边界+1+左子树节点数目 开始到 右边界」的元素就对应了中序遍历中「从 根节点定位+1 到 右边界」的元素
        root.right = myBuildTree(preorder, inorder, preorder_left + size_left_subtree + 1, preorder_right, inorder_root + 1, inorder_right);
        return root;
    }
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        int n = preorder.length;
        // 构造哈希映射,帮助我们快速定位根节点
        indexMap = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) indexMap.put(inorder[i], i);
        return myBuildTree(preorder, inorder, 0, n - 1, 0, n - 1);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-05-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
ShenduCC
2018/07/24
2790
Leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 根据先序和中序遍历还原二叉树。 思路很简单,先序中的第一个点必然为root,所以只要以先序的第一个元素在中序中的位置为分界线,左边为左子树,右边为右子树,递归下去就行 然而我遇到了问题,这是第一个版本,通过MLE了,我想应该是
triplebee
2018/01/12
4500
[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/ 难度:Medium 题目:106. Construct Binary Tree from Inorder and Postorder Traversal
尾尾部落
2018/09/04
5070
leetcode: 105. Construct Binary Tree from Preorder and Inorder Traversal
Difficulty Medium. Problem Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the follow
JNingWei
2019/02/25
4720
55 Construct Binary Search Tree from Preorder Traversal
Return the root node of a binary search tree that matches the given preorder traversal.
devi
2021/08/18
3060
Leetcode Golang 105. Construct Binary Tree from Preorder and Inorder Traversal.go
版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/88935501
anakinsun
2019/04/12
5170
关关的刷题日记97 – Leetcode 105. Construct Binary Tree
关关的刷题日记97 – Leetcode 105. Construct Binary Tree 题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路 给出二叉树的先根遍历和中根遍历,构造二叉树。 了解先根遍历和中根遍历的特点。每次在先根遍历数组中最左边的就是二叉树的根,然后去中
WZEARW
2018/04/12
7510
Leetcode 题目解析之 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
ruochen
2022/01/08
1.4K0
​LeetCode刷题实战105:从前序与中序遍历序列构造二叉树
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
程序员小猿
2021/01/19
2100
​LeetCode刷题实战105:从前序与中序遍历序列构造二叉树
Binary Tree Postorder Traversal — LeetCode
大家好,又见面了,我是你们的朋友全栈君。 原题链接: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 跟 Binary Tree Inorder Traversal 以及 Binary Tree Preorder Traversal 一样,二叉树的后序遍历我们还是介绍三种方法,第一种是递归,第二种是迭代方法,第三种是用线索二叉树。 递归还是那么简单,算法的时间复杂度是O(n), 而空间复杂度则是递归栈的大小,即O(logn)。代码如下:
全栈程序员站长
2022/11/17
2290
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. * struct TreeNode { * int val;
triplebee
2018/01/12
6200
Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/82191052
Tyan
2019/05/26
4010
☆打卡算法☆LeetCode 105、从前序与中序遍历序列构造二叉树 算法解析
“给定两个整数数组pre和ino,其中pre是二叉树的先序遍历,ino是二叉树的中序遍历,构造二叉树返回其根节点。”
恬静的小魔龙
2022/08/07
2540
☆打卡算法☆LeetCode 105、从前序与中序遍历序列构造二叉树  算法解析
剑指offer | 面试题6:重建二叉树
前序遍历性质:节点按照 [ 根节点 | 左子树 | 右子树 ] 排序。中序遍历性质:节点按照 [ 左子树 | 根节点 | 右子树 ] 排序。
千羽
2021/12/29
2060
剑指offer | 面试题6:重建二叉树
【算法题解】 Day30 搜索与回溯
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
sidiot
2023/08/31
1310
【算法题解】 Day30 搜索与回溯
[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal [LeetCode] Construct Bina
链接:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/ 难度:Medium 题目:105. Construct Binary Tree from Preorder and Inorder Traversal
尾尾部落
2018/09/04
3410
算法:分治
分治是一种将大问题分解成相同任务的小问题的方法,常见的分治思想之一就是归并排序(mergeSort)
用户3578099
2022/04/18
1K0
算法:分治
Leetcode: Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree.
卡尔曼和玻尔兹曼谁曼
2019/01/22
4520
二叉树的构建(已知两个遍历结果,来构建二叉树)
假如有这样一棵二叉树,它的前序遍历为1 2 4 5 3 6 ,中序遍历为 4 2 5 1 6 3
IsLand1314
2024/10/15
1390
二叉树的构建(已知两个遍历结果,来构建二叉树)
LeetCode <dfs>105&106 Construct Binary Tree
Given preorder and inorder traversal of a tree, construct the binary tree.
大学里的混子
2018/11/12
5180
推荐阅读
相关推荐
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验