当前位置:网站首页>Arbre binaire pour résoudre le problème (2)
Arbre binaire pour résoudre le problème (2)
2022-07-02 04:18:00 【Hibiscus jasminoides】
Leetcode 226._Retourner l'arbre binaire
// Méthode 1:Traverser l'arbre binaire
public TreeNode InvertTree(TreeNode root)
{
// Traverser l'arbre binaire,Échanger les noeuds enfants de chaque noeud
Traverse(root);
return root;
}
// Fonction de traversée de l'arbre binaire
void Traverse(TreeNode root) {
if (root == null) return;
// Tout ce que chaque noeud doit faire, c'est échanger ses enfants de gauche et de droite
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
// Traverser le cadre,Pour traverser les noeuds des sous - arbres gauche et droit
Traverse(root.left);
Traverse(root.right);
}
// Méthode 2:Problème de décomposition de la fonction récursive
public TreeNode InvertTree(TreeNode root)
{
if (root == null) return root;
// Retourner les sous - arbres gauche et droit
TreeNode left = InvertTree(root.left);
TreeNode right = InvertTree(root.right);
// Échange de sous - noeuds gauche et droit
root.left = right;
root.right = left;
// Par root L'arbre binaire à la racine a été renversé,Retour root
return root;
}Leetcode 116._Remplissez le pointeur de noeud droit suivant pour chaque noeud
public Node Connect(Node root)
{
if (root == null) return null;
Traverse(root.left,root.right);
return root;
}
void Traverse(Node left,Node right)
{
if (left == null|| right == null) return;
// Mettez les deux noeuds entrants ensemble
left.next = right;
// Connectez deux noeuds enfants du même noeud parent
Traverse(left.left,left.right);
Traverse(right.left, right.right);
// Connectez deux noeuds enfants qui chevauchent le noeud parent
Traverse(left.right, right.left);
}Leetcode 114._L'arbre binaire s'agrandit en liste liée
public void Flatten(TreeNode root)
{
if (root == null) return;
Traverse(root);
}
void Traverse(TreeNode root)
{
if (root == null) return;
// En traversant les sous - arbres à gauche et à droite
Traverse(root.left);
Traverse(root.right);
// Position de la séquence arrière
TreeNode left = root.left;
TreeNode right = root.right;
// Prenez le Sous - arbre gauche comme sous - arbre droit
root.left = null;
root.right = left;
// Ajouter le Sous - arbre droit à la fin du sous - arbre droit actuel
TreeNode p = root;
while (p.right!=null)
{
p = p.right;
}
p.right = right;
}边栏推荐
- Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
- SQL:常用的 SQL 命令
- 【c语言】基础篇学习笔记
- Spring moves are coming. Watch the gods fight
- 60后关机程序
- 整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
- First acquaintance with string+ simple usage (II)
- Pytorch---使用Pytorch进行鸟类的预测
- Go variables and constants
- Recyclerview add header
猜你喜欢

Exposure X8标准版图片后期滤镜PS、LR等软件的插件

go 包的使用
![[C language] Dynamic Planning --- from entry to standing up](/img/7e/29482c8f3970bb1a40240e975ef97f.png)
[C language] Dynamic Planning --- from entry to standing up

Use of go package

The solution to the complexity brought by lambda expression
![[C language] basic learning notes](/img/d2/1aeb2d37d97b9cfe4b21aa3ac37645.png)
[C language] basic learning notes

Sword finger offer II 006 Sort the sum of two numbers in the array

【c语言】动态规划---入门到起立

Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)

What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
随机推荐
Three ways for programmers to learn PHP easily and put chaos out of order
Delete the code you wrote? Sentenced to 10 months!
Pytorch---使用Pytorch实现U-Net进行语义分割
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
Common locks in MySQL
Pytorch-Yolov5從0運行Bug解决:
XSS prevention
Go language naming specification
Thinkphp6 limit interface access frequency
Spring moves are coming. Watch the gods fight
[C language] Dynamic Planning --- from entry to standing up
【leetcode】34. Find the first and last positions of elements in a sorted array
Dare to go out for an interview without learning some distributed technology?
First acquaintance with P4 language
LxC limits the number of CPUs
Feature Engineering: summary of common feature transformation methods
微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
Homework of the 16th week
10 minutes to understand CMS garbage collector in JVM