当前位置:网站首页>Collection of binary tree topics
Collection of binary tree topics
2022-07-24 07:41:00 【yn20000227】
One 、 Sequence traversal of binary tree ( Breadth search ) Print binary tree from top to bottom
Using queue First in, first out principle :
class Solution:
def levelOrder(self, root: TreeNode) -> List[int]:
if not root:
return []
List ,queue= [], collections.deque()
queue.append(root)
while(queue):
node = queue.popleft()
List.append(node.val)
if node.left !=None:
queue.append(node.left)
if node.right !=None:
queue.append(node.right)
return ListTwo 、 Binary tree output by layer Print binary tree from top to bottom II
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
List = []
que = collections.deque()
que.append(root)
while(que):
sublist = []
for node in range(len(que)):
node = que.popleft()
sublist.append(node.val)
if node.left:
que.append(node.left)
if node.right:
que.append(node.right)
List.append(sublist)
return List3、 ... and 、 Zigzag Print binary tree : Print binary tree from top to bottom III
1. According to the binary tree sequence traversal method : Even level flashback output , Odd layer positive sequence output ;( Method 1 )
2. Use two deque , When it is an even number of layers , Use left insert ; Insert on the right when there are odd layers
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
List,que = [],collections.deque()
que.append(root)
high = 0
while(que):
high+=1
sublist = collections.deque()
for _ in range(len(que)):
node = que.popleft()
if high%2==0:
sublist.appendleft(node.val)
else:
sublist.append(node.val)
if node.left:
que.append(node.left)
if node.right:
que.append(node.right)
List.append(list(sublist))
return List边栏推荐
- Generate API documents using swagger2markup
- AMD64 (x86_64) architecture ABI document: upper
- C语言文件操作
- Appium use
- Who can stand it when the project goes online
- 【Pytorch】conv2d torchvision.transforms
- Harbor2.2 用户角色权限速查
- Math。 Round, numeric rounding, underlying code parsing
- DevOps随笔
- Stable TTL serial port rate supported by Arduino under different dominant frequencies
猜你喜欢

sqli-labs简单安装

About the solution of thinking that you download torch as a GPU version, but the result is really a CPU version

About using the alignment function of VMD

InjectFix原理学习(实现修复加法的热更)

Laplace distribution

Requests crawler multi page crawling to KFC restaurant location

Selenium basic knowledge debugging method

MySQL queries all parents of the current node

C language advanced part VII. Program compilation and preprocessing

XSS vulnerability learning
随机推荐
Jackson 解析 JSON 详细教程
Induction, generalization, deduction
R language handwritten numeral recognition
【Pytorch】conv2d torchvision.transforms
Single Gmv has increased 100 times. What is the "general rule" behind the rise of popular brands?
Selenium basic knowledge automatic login QQ space
C language advanced part VII. Program compilation and preprocessing
Amber tutorial A17 learning - concept
Install librosa using Tsinghua image
[Huawei] Huawei machine test question-105
Vulnhub DC1
Math。 Round, numeric rounding, underlying code parsing
25.消息订阅与发布——PubSub-js
归纳、概括、演绎
Installation and use of Zen path & defect report & defect operation
Sword finger offer special assault edition day 8
Good programmers and bad programmers
GBK code in idea is converted to UTF-8 format ctrl+c+v one second solution perfect solution for single code file escape
Deep learning two or three things - review those classical convolutional neural networks
【Pytorch】nn.Module