【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
题目描述
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
题目大意
使用先序遍历和中序遍历序列重构二叉树。
解题方法
递归
解决本题需要牢牢掌握先序遍历和中序遍历的含义,以及递归。
先序遍历:根节点,左子树的先序遍历,右子树的先序遍历。
中序遍历:左子树的中序遍历,根节点,右子树的中序遍历。
可以看出,先序遍历的开头第一个元素是根元素。找到根节点在中序遍历中的位置,则可以利用根节点将中序遍历的数组分割出左右子树。再根据左右子树的长度对先序遍历的数组切片。使用递归则可以构建出完整的树。
很多朋友不理解递归,这里多说一下怎么理解递归。首先一定要明确递归函数的定义、其输入和输出是什么,而不用明白该函数内部具体是怎么实现的。我们将递归函数当做黑盒使用,也当做普通函数使用,一定不要试图用大脑理解该递归函数内部是怎么递归的,很容易绕进去。即,我不需要知道这个函数怎么实现的,我调用这个函数就是能实现某个功能。
比如对于本题而言,buildTree(preorder, inorder)
函数的输入是一棵树的先序遍历序列和中序遍历序列,该函数的返回值是构建好的这棵树的根节点。我们在找到root
节点后,设定其左右子树时,依然调用buildTree(preorder, inorder)
,此时的输入变成了root
左右子树对应的先序遍历和中序遍历序列,该函数的返回值就是构建好的左右子树的根节点。
至此代码就写完了,我们看到构建 root
的左右子树时,直接使用buildTree(preorder, inorder)
函数,只要给定正确的输入,这个函数就能给正确的输出,不用去向这个函数到底干了什么。
这样理解递归是不是更清晰了呢?
Python代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
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[0])
index = inorder.index(preorder[0])
root.left = self.buildTree(preorder[1:index+1], inorder[:index])
root.right = self.buildTree(preorder[index+1:], inorder[index+1:])
return root
日期
2018 年 6 月 22 日 —— 这周的糟心事终于完了
2019 年 1 月 8 日 —— 别熬夜,我都开始有黑眼圈了。。
2020 年 5 月 22 日 —— 这天的每日一题,熬夜写题解,也会黑眼圈。
【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)的更多相关文章
- 105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树
给定一棵树的前序遍历与中序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出前序遍历 = [3,9,20,15,7]中序遍历 = [9,3,15,20,7]返回如下的二叉树: ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
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 t ...
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...
- [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 ...
- (二叉树 递归) 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 ...
- [Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- 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 ...
- leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- Linux—查看内核版本、系统版本、系统位数
一.查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@crowe. ...
- 苹果ios通过描述文件获取udid
苹果ios通过描述文件获取udid 需要准备的东西 1,安装描述文件只支持https的回调地址,所以需要申请https域名 2,描述文件签名,不安装也可,只要能接受红色的字 步骤: 1,准备xml文件 ...
- lua_newthread的真正意义
lua_newthread 这个接口,存在误导性,很多人第一次试图用它来解决多线程问题时,都会入坑. 实际上,这个接口真正的用法,是给那些在lua更底层的某些行为(通常是递归)导致了lua的栈溢出而准 ...
- 12 — springboot集成JPA — 更新完毕
1.什么是jpa? 一堆不想整在这博客里面的理论知识.这些理论玩意儿就应该自行领悟到自己脑海里 1).JPA & Spring Data JPA 1.1).JPA JPA是Java Persi ...
- 网易云信 集成UI库登录dologin没有回调
感谢github上的两位大佬指出问题的解决方法. 解决方法: 在进行ui初始化要在主进程中进行,初始化前进行主进程判断. 若还收不到回调,可尝试将uikit中的base包去掉而在build.gradl ...
- 大规模 K8s 集群管理经验分享 · 上篇
11 月 23 日,Erda 与 OSCHINA 社区联手发起了[高手问答第 271 期 -- 聊聊大规模 K8s 集群管理],目前问答活动已持续一周,由 Erda SRE 团队负责人骆冰利为大家解答 ...
- C++11的auto自动推导类型
auto是C++11的类型推导关键字,很强大 例程看一下它的用法 #include<vector> #include<algorithm> #include<functi ...
- TCP中的TIME_WAIT状态
TIME_WAIT的存在有两大理由 1.可靠地实现TCP全双工连接的终止 2.允许老的可重复分节在网络中消失. 对于理由1,我们知道TCP结束需要四次挥手,若最后一次的客户端的挥手ACK丢失(假设是客 ...
- ebs 初始化登陆
BEGIN fnd_global.APPS_INITIALIZE(user_id => youruesr_id, esp_id => yourresp_id, resp_appl_id = ...
- EBS 抓trace 文件
如果要对FORM的操作做TRACE操作,可以使用 帮助->诊断->跟踪 中启用跟踪功能来实现. 但是如果要实现对并发请求的trace,需要在 系统管理员->并发->方案-> ...