当前位置:网站首页>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;
}边栏推荐
- How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
- 蓝湖的安装及使用
- Handling of inconsistency between cursor and hinttext position in shutter textfield
- Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
- Major domestic quantitative trading platforms
- unable to execute xxx. SH: operation not permitted
- Hand tear - sort
- Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
- [JS -- map string]
- Demonstration description of integrated base scheme
猜你喜欢

"No war on the Western Front" we just began to love life, but we had to shoot at everything

Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?

Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial

整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
![[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)](/img/e1/620443dbc6ea8b326e1242f25d6d74.jpg)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)

Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial

Typescript practice for SAP ui5

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

QT designer plug-in implementation of QT plug-in

【c语言】基础篇学习笔记
随机推荐
go 包的使用
C language practice - number guessing game
Pytorch---使用Pytorch实现U-Net进行语义分割
Target free or target specific: a simple and effective zero sample position detection comparative learning method
Bitmap principle code record
Hand tear - sort
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
[untitled]
Pytorch---使用Pytorch进行图像定位
Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
[improvement class] st table to solve the interval maximum value problem [2]
LxC limits the number of CPUs
Dare to go out for an interview without learning some distributed technology?
office_ Delete the last page of word (the seemingly blank page)
Yyds dry inventory compiler and compiler tools
【leetcode】34. Find the first and last positions of elements in a sorted array
Which product of anti-cancer insurance is better?
Pytoch --- use pytoch to predict birds
Wechat applet JWT login issue token
QT designer plug-in implementation of QT plug-in