当前位置:网站首页>二叉树创建 & 遍历
二叉树创建 & 遍历
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......
边栏推荐
- 【mysql学习笔记30】锁(非教程)
- Typescript function definition
- How Navicat imports MySQL scripts
- Description of octomap averagenodecolor function
- How to prevent Association in cross-border e-commerce multi account operations?
- How MySQL merges data
- Nc204382 medium sequence
- Type of data in energy dashboard
- 链表面试题(图文详解)
- opencv学习笔记八--答题卡识别
猜你喜欢

How to prevent Association in cross-border e-commerce multi account operations?
![[MySQL learning notes 30] lock (non tutorial)](/img/9b/1e27575d83ff40bebde118b925f609.png)
[MySQL learning notes 30] lock (non tutorial)

leecode-C语言实现-15. 三数之和------思路待改进版

合规、高效,加快药企数字化转型,全新打造药企文档资源中心
![If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]](/img/57/12a97ab3d2dabfaf06bbe1788450cf.png)
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
![[非线性控制理论]9_非线性控制理论串讲](/img/a8/03ed363659a0a067c2f1934457c106.png)
[非线性控制理论]9_非线性控制理论串讲

In the era of digital economy, how to ensure security?
![[cf gym101196-i] waif until dark network maximum flow](/img/66/6b339fc23146b5fbdcd2a1fa0a2349.png)
[cf gym101196-i] waif until dark network maximum flow

leecode-C語言實現-15. 三數之和------思路待改進版

Google可能在春节后回归中国市场。
随机推荐
Fundamentals of C language 9: Functions
解决方案:智慧工地智能巡检方案视频监控系统
Bit operation XOR
Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
onie支持pice硬盘
http缓存,强制缓存,协商缓存
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
Nc204382 medium sequence
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
TypeScript void 基础类型
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
Methods for JS object to obtain attributes (. And [] methods)
Summary of Digital IC design written examination questions (I)
C # connect to SQLite database to read content
Get the path of edge browser
Luogu p4127 [ahoi2009] similar distribution problem solution
How can word delete English only and keep Chinese or delete Chinese and keep English
学go之路(二)基本类型及变量、常量
How to estimate the number of threads
位运算异或