当前位置:网站首页>二叉树创建 & 遍历
二叉树创建 & 遍历
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......
边栏推荐
- Database addition, deletion, modification and query
- DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
- Generator Foundation
- Simulation of Teman green interferometer based on MATLAB
- [window] when the Microsoft Store is deleted locally, how to reinstall it in three steps
- Get/post/put/patch/delete meaning
- Ble of Jerry [chapter]
- opencv学习笔记八--答题卡识别
- Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
- Key value judgment in the cycle of TS type gymnastics, as keyword use
猜你喜欢
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
Simulation of Michelson interferometer based on MATLAB
How Navicat imports MySQL scripts
[非线性控制理论]9_非线性控制理论串讲
leecode-C語言實現-15. 三數之和------思路待改進版
NiO programming introduction
数字经济时代,如何保障安全?
【MySQL学习笔记32】mvcc
861. Score after flipping the matrix
随机推荐
How to estimate the number of threads
Simple and understandable high-precision addition in C language
xpath中的position()函数使用
2022年Instagram运营小技巧简单讲解
1015 reversible primes (20 points) prime d-ary
qt颜色与字符串、uint相互转换
[window] when the Microsoft Store is deleted locally, how to reinstall it in three steps
NiO programming introduction
Methods for JS object to obtain attributes (. And [] methods)
Luogu p4127 [ahoi2009] similar distribution problem solution
onie支持pice硬盘
PHP Coding Standard
Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics
Typescript interface properties
How Navicat imports MySQL scripts
word中把帶有某個符號的行全部選中,更改為標題
C # connect to SQLite database to read content
Opencv learning notes 8 -- answer sheet recognition
珠海金山面试复盘
How can word delete English only and keep Chinese or delete Chinese and keep English