LeetCode-105:Construct Binary Tree from Preorder and Inorder Traversal (利用先序和中序遍历构建二叉树) -- medium
来源:互联网 发布:手机能申请淘宝店铺吗 编辑:程序博客网 时间:2024/06/05 09:09
Question
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
- You may assume that duplicates do not exist in the tree.
问题解析:
给定一棵树的先序和中序遍历数组,构建该二叉树。
Answer
Solution 1:
数据结构,递归调用。
与LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal的思想是相同的。
- 利用先序遍历的最先的一个元素为子树的根结点,该根结点在中序遍历的位置为左右子树的分割点。
- 需要注意左右子树的边界。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if (preorder == null || inorder == null || preorder.length != inorder.length) return null; Map<Integer, Integer> inMap = new HashMap<Integer, Integer>(); for(int i = 0; i < inorder.length; i++) { inMap.put(inorder[i], i); } TreeNode root = buildTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, inMap); return root; } public TreeNode buildTree(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, Map<Integer, Integer> inMap) { if(preStart > preEnd || inStart > inEnd) return null; TreeNode root = new TreeNode(preorder[preStart]); int inRoot = inMap.get(root.val); int numsLeft = inRoot - inStart; root.left = buildTree(preorder, preStart + 1, preStart + numsLeft, inorder, inStart, inRoot - 1, inMap); root.right = buildTree(preorder, preStart + numsLeft + 1, preEnd, inorder, inRoot + 1, inEnd, inMap); return root; }}
- 时间复杂度:O(lgn),空间复杂度:O(n)
Solution 2:
python 解法,更加简洁。
- 以preorder从前到后一直保存的是子树的根结点,所以取根结点直接pop(0),先建立左子树,再建立右子树。
- 更加清晰简洁。
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def buildTree(self, preorder, inorder): """ :type preorder: List[int] :type inorder: List[int] :rtype: TreeNode """ if not preorder or not inorder: return None root = TreeNode(preorder.pop(0)) preorderindex = inorder.index(root.val) root.left = self.buildTree(preorder, inorder[:preorderindex]) root.right = self.buildTree(preorder, inorder[preorderindex+1:]) return root
阅读全文
0 0
- LeetCode-105:Construct Binary Tree from Preorder and Inorder Traversal (利用先序和中序遍历构建二叉树) -- medium
- 105.leetcode Construct Binary Tree from Preorder and Inorder Traversal(medium)[先序、中序构造二叉树]
- LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 根据先序序列和中序序列恢复二叉树
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序遍历构建二叉树
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal 先序和中序中恢复二叉树 解题报告
- Construct Binary Tree from Preorder and Inorder Traversal 根据先序和中序序列生成二叉树(重重)
- Construct Binary Tree from Preorder and Inorder Traversal-先序和中序构造树
- [LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal 根据前序遍历和中序遍历构造二叉树
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序重建二叉树
- leetCode 105.Construct Binary Tree from Preorder and Inorder Traversal (根据前序遍历和中序遍历构造二叉树)
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal(从前序遍历和中序遍历构造二叉树)
- Construct Binary Tree from Preorder and Inorder Traversal 从先序和中序重建二叉树@LeetCode
- LeetCode105--Tree--Construct Binary Tree from Preorder and Inorder Traversal--根据前序遍历+中序遍历构建树
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树
- Leetcode Construct Binary Tree from Preorder and Inorder Traversal 前序中序遍历重组二叉树
- 利用NPAPI开发Chrome插件
- redis哈希相关命令
- PHPSTORM插件
- 第七周项目三
- 【数据库】FaceDataset常用的人脸数据库
- LeetCode-105:Construct Binary Tree from Preorder and Inorder Traversal (利用先序和中序遍历构建二叉树) -- medium
- JQuery基础笔记
- 大数据早报:Facebook宣布加入Open Media开源影音联盟 Bossa Nova融资1750万美元 (11.16)
- 小凯称棒子
- iOS自定义从底部弹上来的View
- less 基础 边学边整理
- 对于机器学习,到底该选择哪种编程语言
- 九周 项 目3 利用二叉树遍历思想解决问题
- Navicat 提示Cannot create oci environment 解决方案