当前位置:网站首页>二叉树创建 & 遍历
二叉树创建 & 遍历
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......
边栏推荐
- C intercept string
- Basics of reptile - Scratch reptile
- Description of octomap averagenodecolor function
- The way to learn go (II) basic types, variables and constants
- Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
- 数字经济时代,如何保障安全?
- NiO programming introduction
- Simulation of Teman green interferometer based on MATLAB
- (lightoj - 1410) consistent verbs (thinking)
- Relevant introduction of clip image
猜你喜欢
![Ble of Jerry [chapter]](/img/00/27486ad68bf491997d10e387c32dd4.png)
Ble of Jerry [chapter]

word中如何删除某符号前面或后面所有的文字

How MySQL merges data

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

继电反馈PID控制器参数自整定

Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume

解决方案:智慧工地智能巡檢方案視頻監控系統

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

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

Generator Foundation
随机推荐
TypeScript 函数定义
Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
The way to learn go (II) basic types, variables and constants
Solution: intelligent site intelligent inspection scheme video monitoring system
Position() function in XPath uses
Google可能在春节后回归中国市场。
Iterator Foundation
In the era of digital economy, how to ensure security?
解决方案:智慧工地智能巡检方案视频监控系统
word中把帶有某個符號的行全部選中,更改為標題
软件开发的一点随记
[cf gym101196-i] waif until dark network maximum flow
xpath中的position()函数使用
超级浏览器是指纹浏览器吗?怎样选择一款好的超级浏览器?
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
Jerry's general penetration test - do data transmission with app Communication [article]
How to delete all the words before or after a symbol in word
[MySQL learning notes 29] trigger
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在
Typescript void base type