首页 > 其他分享 >LeetCode 105. 从前序与中序遍历序列构造二叉树

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

时间:2023-05-19 22:16:07浏览次数:42  
标签:遍历 TreeNode int root 中序 二叉树 inorder LeetCode

题目链接:LeetCode 105. 从前序与中序遍历序列构造二叉树

题意:

给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。

解题思路:

模拟手动构建的过程,注意下标的变化。

完整代码如下:

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
var (
    hash map[int]int
)
func buildTree(preorder []int, inorder []int) *TreeNode {
    hash = make(map[int]int, len(inorder))
    for i, v := range inorder {
        hash[v] = i
    }
    root := build(preorder, inorder, 0, 0, len(inorder)-1)  // l, r 表示构造的树在中序遍历数组中的范围
    return root
}
func build(pre []int, in []int, root int, l, r int) *TreeNode {
    if l > r {
        return nil
    }
    rootVal := pre[root]  // 找到本次构造的树的根节点
    index := hash[rootVal]  // 根节点在中序数组中的位置
    node := &TreeNode {Val: rootVal}
    node.Left = build(pre, in, root + 1, l, index-1)
    node.Right = build(pre, in, root + (index-l) + 1, index+1, r)
    return node
}

标签:遍历,TreeNode,int,root,中序,二叉树,inorder,LeetCode
From: https://www.cnblogs.com/lxing-go/p/17416423.html

相关文章

  • LeetCode 113. 路径总和 II
    题目链接:LeetCode113.路径总和II题意:给你二叉树的根节点root和一个整数目标和targetSum,找出所有从根节点到叶子节点路径总和等于给定目标和的路径。解题思路:与LeetCode112.路径总和相似,在遍历过程中,记录遍历过的每一个点即可。递归法递归代码:varres[][]intva......
  • LeetCode 513. 找树左下角的值
    题目链接:LeetCode513.找树左下角的值题意:给定一个二叉树的根节点root,请找出该二叉树的最底层最左边节点的值。解题思路:首先明确本题是要找最底层的最左边的节点,因此迭代法,可以采用层次遍历,res每次记录每一层的最左边的节点,当遍历结束时,res表示的就是最底层,最左边的节......
  • LeetCode/子数组的最小值之和
    给定一个整数数组arr,找到min(b)的总和,其中b的范围为arr的每个(连续)子数组。1.单调栈假如要遍历所有区间,哪怕可以直接获得最小值,时间复杂度也是O(n2)这里我们不逐个找对应区间,而是计算每个值对区间的贡献,可以将时间复杂度降到O(n)其实也就找遍历时当前值的左边界和右边界,在......
  • [LeetCode] 1079. Letter Tile Possibilities
    Youhave n  tiles,whereeachtilehasoneletter tiles[i] printedonit.Return thenumberofpossiblenon-emptysequencesofletters youcanmakeusingthelettersprintedonthose tiles.Example1:Input:tiles="AAB"Output:8Explanation:......
  • LeetCode/完成任务的最少工作时间段
    一个工作时间段可以连续工作sessiontime个小时给你任务列表task,task[i]表示第i项任务花费时间求完成全部工作所需最小时间段(可以按任意顺序完成任务)1.回溯法回溯时按任务下标推进,边界条件为任务下标等于任务长度同时要记录回溯几个状态,分别是当前任务下标、已用时间段、各......
  • 二刷Leetcode-Days06
    二叉树:/***迭代法实现中序前序后序遍历*@paramroot*@return*/publicList<Integer>preorderTraversalIterator(TreeNoderoot){List<Integer>result=newArrayList<>();if(root==null){ret......
  • Uva--548 Tree(三个递归遍历/重建二叉树)
    记录23:132023-5-18uva.onlinejudge.org/external/5/548.htmlreference:《算法竞赛入门经典第二版》例题6-8使用中序遍历和后序遍历还原二叉树,还行,还是熟悉的。收获的点:使用数组快速建立二叉树(还是要变通,《数据结构与算法分析》中标准的使用了结构体指针,太过学术了?函数......
  • #yyds干货盘点# LeetCode程序员面试金典: 二叉树的层序遍历 II
    1.简述:给你二叉树的根节点root,返回其节点值自底向上的层序遍历。(即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 示例1:输入:root=[3,9,20,null,null,15,7]输出:[[15,7],[9,20],[3]]示例2:输入:root=[1]输出:[[1]]示例3:输入:root=[]输出:[]2.代码实现:classSolution......
  • leetcode-1207-easy
    UniqueNumberofOccurencesGivenanarrayofintegersarr,returntrueifthenumberofoccurrencesofeachvalueinthearrayisuniqueorfalseotherwise.Example1:Input:arr=[1,2,2,1,1,3]Output:trueExplanation:Thevalue1has3occurrences,......
  • leetcode-1013-easy
    PartitionArrayIntoThreePartsWithEqualSumGivenanarrayofintegersarr,returntrueifwecanpartitionthearrayintothreenon-emptypartswithequalsums.Formally,wecanpartitionthearrayifwecanfindindexesi+1<jwith(arr[0]+......