当前位置:网站首页>Binary tree creation & traversal
Binary tree creation & traversal
2022-07-06 07:46:00 【Wang jijiya】
Hierarchy creation
Reference resources :Python --- Sequence establishment of binary tree
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] # Used to store nodes in the tree , Every time a node is generated, it is placed in the list , Save the root node here .
j = 1 # Create variables j be used for nodeList The index of the element in the , For the initial 1. Because the first 0 Elements have created root nodes .
for node in nodes: # Take out one by one Nodes Nodes in the list , Create a left child node and a right child node
if node != None:
node.left = (TreeNode(nodeList[j])) if nodeList[j] != None else None
nodes.append(node.left) # Add the new node Nodes Array , Make it for In the loop, you can continue to add child nodes
j += 1
if j == len(nodeList): # If the nodeList Equal values indicate that all nodes have been added , Go straight back to head The root node completes the establishment of the tree
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)
# Use : Return the longest path of binary tree ,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......
边栏推荐
- When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
- Emo diary 1
- Document 2 Feb 12 16:54
- Position() function in XPath uses
- How to delete all the words before or after a symbol in word
- [MySQL learning notes 29] trigger
- C # display the list control, select the file to obtain the file path and filter the file extension, and RichTextBox displays the data
- Basics of reptile - Scratch reptile
- How to prevent Association in cross-border e-commerce multi account operations?
- Qualitative risk analysis of Oracle project management system
猜你喜欢
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
TypeScript接口与泛型的使用
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
MEX有关的学习
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
解决方案:智慧工地智能巡检方案视频监控系统
数字经济时代,如何保障安全?
数据治理:主数据的3特征、4超越和3二八原则
Opencv learning notes 9 -- background modeling + optical flow estimation
随机推荐
Google可能在春节后回归中国市场。
opencv学习笔记八--答题卡识别
Ble of Jerry [chapter]
Typescript interface and the use of generics
jmeter性能测试步骤实战教程
软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历
MES, APS and ERP are essential to realize fine production
2022年Instagram运营小技巧简单讲解
合规、高效,加快药企数字化转型,全新打造药企文档资源中心
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
Iterator Foundation
Word setting directory
Sélectionnez toutes les lignes avec un symbole dans Word et changez - les en titre
Linked list interview questions (Graphic explanation)
TS 类型体操 之 循环中的键值判断,as 关键字使用
Notes on software development
How to estimate the number of threads
超级浏览器是指纹浏览器吗?怎样选择一款好的超级浏览器?
Mise en œuvre du langage leecode - C - 15. Somme des trois chiffres - - - - - idées à améliorer