当前位置:网站首页>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......
边栏推荐
- qt颜色与字符串、uint相互转换
- Sélectionnez toutes les lignes avec un symbole dans Word et changez - les en titre
- [computer skills]
- Sharing of source code anti disclosure scheme under burning scenario
- 2022年Instagram运营小技巧简单讲解
- Helm install Minio
- 1015 reversible primes (20 points) prime d-ary
- Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
- Bugku CTF daily question: do you want seeds? Blackmailed
- Le chemin du navigateur Edge obtient
猜你喜欢

Summary of Digital IC design written examination questions (I)

In the era of digital economy, how to ensure security?
![Ble of Jerry [chapter]](/img/00/27486ad68bf491997d10e387c32dd4.png)
Ble of Jerry [chapter]

jmeter性能测试步骤实战教程

Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
![[redis] Introduction to NoSQL database and redis](/img/95/229d7a08e94245f2733b8c59201cff.png)
[redis] Introduction to NoSQL database and redis

esRally国内安装使用避坑指南-全网最新
![[count] [combined number] value series](/img/f5/6fadb8f1c8b75ddf5994c2c43feaa6.jpg)
[count] [combined number] value series

How to prevent Association in cross-border e-commerce multi account operations?

链表面试题(图文详解)
随机推荐
edge浏览器 路径获得
[非线性控制理论]9_非线性控制理论串讲
[computer skills]
In the era of digital economy, how to ensure security?
Type of data in energy dashboard
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在
Relevant introduction of clip image
烧录场景下的源代码防泄密方案分享
C # connect to SQLite database to read content
数据治理:元数据管理篇
(lightoj - 1410) consistent verbs (thinking)
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
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
861. Score after flipping the matrix
Scala语言学习-08-抽象类
After the hot update of uniapp, "mismatched versions may cause application exceptions" causes and Solutions
Generator Foundation
Ble of Jerry [chapter]
Mex related learning
Emo diary 1