当前位置:网站首页>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......
边栏推荐
- Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
- The way to learn go (II) basic types, variables and constants
- [MySQL learning notes 32] mvcc
- Games101 Lesson 7 shading 1 Notes
- Solution: intelligent site intelligent inspection scheme video monitoring system
- Wonderful use of TS type gymnastics string
- edge浏览器 路径获得
- [KMP] template
- Luogu p4127 [ahoi2009] similar distribution problem solution
- Position() function in XPath uses
猜你喜欢

数据治理:主数据的3特征、4超越和3二八原则

Apache middleware vulnerability recurrence

Typescript interface and the use of generics

Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
![[MySQL learning notes 30] lock (non tutorial)](/img/9b/1e27575d83ff40bebde118b925f609.png)
[MySQL learning notes 30] lock (non tutorial)

Bugku CTF daily question: do you want seeds? Blackmailed

Basics of reptile - Scratch reptile

jmeter性能测试步骤实战教程
![Jerry's ad series MIDI function description [chapter]](/img/28/e0f9b41db597ff3288af431c677001.png)
Jerry's ad series MIDI function description [chapter]

How to delete all the words before or after a symbol in word
随机推荐
Nc204382 medium sequence
Leecode-c language implementation -15 Sum of three ----- ideas to be improved
xpath中的position()函数使用
Brief explanation of instagram operation tips in 2022
Apache middleware vulnerability recurrence
2022年Instagram运营小技巧简单讲解
Three treasures of leeks and Chinese men's football team
Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics
Scala language learning-08-abstract classes
解决方案:智慧工地智能巡檢方案視頻監控系統
软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
49. Sound card driven article collection
Ble of Jerry [chapter]
Document 2 Feb 12 16:54
[computer skills]
TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
Generator Foundation
Methods for JS object to obtain attributes (. And [] methods)
[dictionary tree] [trie] p3879 [tjoi2010] reading comprehension