当前位置:网站首页>二叉树创建 & 遍历
二叉树创建 & 遍历
2022-07-06 07:40:00 【王吉吉丫】
层次创建
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = None
self.right = None
def creatTree(nodeList):
if nodeList[0] == None: return None
head = TreeNode(nodeList[0])
nodes = [head] # 用于存放树中的节点,每生成一个节点就将其放入该列表中,此处将根节点存入。
j = 1 # 创建变量j用于nodeList中元素的索引,初始为1.因为第0个元素已经创建根节点了。
for node in nodes: # 依次取出Nodes列表中的节点,对其创建左子节点和右子节点
if node != None:
node.left = (TreeNode(nodeList[j])) if nodeList[j] != None else None
nodes.append(node.left) # 将新建的节点加入Nodes数组,使其在for循环中可以继续为其添加子节点
j += 1
if j == len(nodeList): # 如果与nodeList值相等说明全部节点已经添加完毕了,直接返回head根节点就完成了树的建立
return head
node.right = (TreeNode(nodeList[j])) if nodeList[j] != None else None
j += 1
nodes.append(node.right)
if j == len(nodeList):
return head
a = [1,2,3,4,None,6,7]
a = creatTree(a)
# 使用:返回二叉树最长路径,Leecode 124
class Solution:
def __init__(self):
self.maxSum = float("-inf")
def maxPathSum(self, root: TreeNode) -> int:
def maxGain(node):
if not node:
return 0
leftGain = max(maxGain(node.left), 0)
rightGain = max(maxGain(node.right), 0)
priceNewpath = node.val + leftGain + rightGain
self.maxSum = max(self.maxSum, priceNewpath)
return node.val + max(leftGain, rightGain)
maxGain(root)
return self.maxSum
s = Solution()
s.maxPathSum(a)Continue......
边栏推荐
- Related operations of Excel
- octomap averageNodeColor函数说明
- Ble of Jerry [chapter]
- Relevant introduction of clip image
- Type of data in energy dashboard
- Openjudge noi 2.1 1749: Digital Square
- C # create database connection object SQLite database
- Force buckle day31
- Wonderful use of TS type gymnastics string
- Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University
猜你喜欢
TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
![[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code](/img/93/ec9de907cae4714038bb5f95aba52b.png)
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code

剪映的相关介绍
![If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]](/img/d6/92ad1c6f84415de6ab0dfd16cd6073.png)
If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]

Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB

qt颜色与字符串、uint相互转换

解决方案:智慧工地智能巡检方案视频监控系统

MEX有关的学习

Apache middleware vulnerability recurrence
随机推荐
2022年Instagram运营小技巧简单讲解
TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
Typescript interface properties
Ble of Jerry [chapter]
Brief explanation of instagram operation tips in 2022
【Redis】NoSQL数据库和redis简介
【mysql学习笔记30】锁(非教程)
Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
Sharing of source code anti disclosure scheme under burning scenario
Typescript void base type
word中把帶有某個符號的行全部選中,更改為標題
Excel的相关操作
Simulation of Teman green interferometer based on MATLAB
TS类型体操 之 字符串的妙用
Description of octomap averagenodecolor function
In the era of digital economy, how to ensure security?
Type of data in energy dashboard
烧录场景下的源代码防泄密方案分享
[MySQL learning notes 32] mvcc
Database addition, deletion, modification and query