当前位置:网站首页>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......
边栏推荐
- Hackathon ifm
- Typescript interface and the use of generics
- Fundamentals of C language 9: Functions
- Description of octomap averagenodecolor function
- 链表面试题(图文详解)
- How to estimate the number of threads
- Google可能在春节后回归中国市场。
- Brief explanation of instagram operation tips in 2022
- Select all the lines with a symbol in word and change them to titles
- 【Redis】NoSQL数据库和redis简介
猜你喜欢
![[factorial inverse], [linear inverse], [combinatorial counting] Niu Mei's mathematical problems](/img/6d/282d904810807810adb06b071fb39b.jpg)
[factorial inverse], [linear inverse], [combinatorial counting] Niu Mei's mathematical problems
![[MySQL learning notes 32] mvcc](/img/0d/2df82b63d1eb3283a84e27f67c1523.png)
[MySQL learning notes 32] mvcc

软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历

Simulation of Michelson interferometer based on MATLAB
![[1. Delphi foundation] 1 Introduction to Delphi Programming](/img/14/272f7b537eedb0267a795dba78020d.jpg)
[1. Delphi foundation] 1 Introduction to Delphi Programming

解决方案:智慧工地智能巡检方案视频监控系统

剪映的相关介绍

Games101 Lesson 7 shading 1 Notes

合规、高效,加快药企数字化转型,全新打造药企文档资源中心

Summary of Digital IC design written examination questions (I)
随机推荐
智能终端设备加密防护的意义和措施
How to estimate the number of threads
洛谷P1836 数页码 题解
Type of data in energy dashboard
Interview Reply of Zhuhai Jinshan
Cf1036c class numbers solution
Codeforces Global Round 19(A~D)
JMeter performance test steps practical tutorial
Solution: intelligent site intelligent inspection scheme video monitoring system
【Redis】NoSQL数据库和redis简介
[1. Delphi foundation] 1 Introduction to Delphi Programming
Sharing of source code anti disclosure scheme under burning scenario
The difference between TS Gymnastics (cross operation) and interface inheritance
[KMP] template
Scala language learning-08-abstract classes
软件开发的一点随记
In the era of digital economy, how to ensure security?
[MySQL learning notes 29] trigger
数据治理:元数据管理篇
Esrally domestic installation and use pit avoidance Guide - the latest in the whole network