LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal-前序中序遍历构造二叉树-Python和Java递归解法
题目地址:Construct Binary Tree from Preorder and Inorder Traversal - LeetCode
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
python递归解法:
# 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: List[int], inorder: List[int]) -> TreeNode:if len(preorder)==0:return Noneroot=TreeNode(preorder[0])middle=inorder.index(preorder[0])root.left=self.buildTree(preorder[1:middle+1],inorder[:middle])root.right=self.buildTree(preorder[middle+1:],inorder[middle+1:])return root
java递归解法:
/*** 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.length == 0) {return null;}TreeNode root = new TreeNode(preorder[0]);int middle = 0;for (int i = 0; i < preorder.length; i++) {if (inorder[i] == root.val) {middle = i;break;}}root.left = buildTree(Arrays.copyOfRange(preorder, 1, middle + 1),Arrays.copyOfRange(inorder, 0, middle));root.right = buildTree(Arrays.copyOfRange(preorder, middle + 1, preorder.length),Arrays.copyOfRange(inorder, middle + 1, preorder.length));return root;}
}
Java迭代做法:
public TreeNode buildTree(int[] preorder, int[] inorder) {if (preorder.length == 0) return null;Stack<TreeNode> s = new Stack<>();TreeNode root = new TreeNode(preorder[0]), cur = root;for (int i = 1, j = 0; i < preorder.length; i++) {if (cur.val != inorder[j]) {cur.left = new TreeNode(preorder[i]);s.push(cur);cur = cur.left;} else {j++;while (!s.empty() && s.peek().val == inorder[j]) {cur = s.pop();j++;}cur = cur.right = new TreeNode(preorder[i]);}}return root;
}
LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal-前序中序遍历构造二叉树-Python和Java递归解法相关推荐
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...
- [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 t ...
- 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 t ...
- [LeetCode] 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 ...
- leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)...
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...
- LeetCode OJ - Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- [leetcode] Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
最新文章
- mysql构架,索引,视图,查询语句
- hibernate---一对一单项外键关联
- memcached 介绍
- 3ds max 渲染清晰面片的边缘
- 博物馆守卫问题(世界名画展览馆)
- 团队作业6——展示博客(Alpha版本)
- 弹性法计算方法的mck法_粘弹性自由阻尼加筋板的随机响应分析和试验研究
- 1059. C语言竞赛(20)
- 任务并行VS数据并行
- win11关机后主机依旧运行怎么办 Windows11关机后主机依旧运行的解决方法
- python 全部缩进一行_Python成为专业人士笔记–程序行空白及tab缩进的作用
- 高等数学(第七版)同济大学 习题7-5 个人解答
- 贪吃蛇-单机游戏-微信小程序项目开发流程详解
- java messagedigest_JAVA MessageDigest(MD5加密等)
- Flex Builder 3 正版注册码及破解版方法
- html5在线拍照,HTML5整合本地摄像头,实现视屏预览及拍照上传
- RabbitMQ安装问题
- 桶装水同城预订下单送水小程序开发制作(水站桶装水配送系统)
- 2021iGEM感想
- 工作笔记-thingworx连接mongdb
热门文章
- PostgreSQL10.5安装后(Win10)环境变量配置与运行
- CLASSPATH的作用
- node 压缩图片_1Mb压缩成100k,图片无损压缩我选择它
- JupyterLab Server 搭建与使用笔记
- Nature综述:从土壤到临床-微生物次级代谢产物对抗生素耐受性和耐药性的影响...
- 宏基因组数据提交GSA实操手册—发表文章前必备技能
- NAR:rrnDB-16S拷贝数校正数据库
- R语言使用ggradar包可视化基本雷达图(radar chart、蜘蛛图spider plot)、可视化单个数据对象的雷达图、自定义雷达图的线条类型、线条宽度、数据点大小、色彩等
- python使用matplotlib对比多个模型的在训练集上的效果并使用柱状图进行可视化:基于交叉验证的性能均值(mean)和标准差(std)进行可视化分析、使用标准差信息添加误差区间条yerr
- R语言ggplot2可视化:为可视化图像添加多行标题(multi line title)并将多行标题居中对齐(center align)