当前位置:网站首页>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;
}边栏推荐
- Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
- Spring moves are coming. Watch the gods fight
- pip 安装第三方库
- Installation et utilisation du lac bleu
- C language: examples of logical operation and judgment selection structure
- SQL: common SQL commands
- go 变量与常量
- 整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
- Today's plan: February 15, 2022
- Go branch and loop
猜你喜欢

C language: examples of logical operation and judgment selection structure

Pytoch --- use pytoch to predict birds

Pytoch --- use pytoch for image positioning
![[JS event -- event flow]](/img/fe/199890b082845f68b65f25056e6f29.jpg)
[JS event -- event flow]

Raspberry pie GPIO pin controls traffic light and buzzer
![[C language] basic learning notes](/img/d2/1aeb2d37d97b9cfe4b21aa3ac37645.png)
[C language] basic learning notes

Delete the code you wrote? Sentenced to 10 months!

CorelDRAW Graphics Suite2022免费图形设计软件

Playing with concurrency: what are the ways of communication between threads?

Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
随机推荐
Force buckle 540 A single element in an ordered array
Wechat applet pull-down loading more waterfall flow loading
66.qt quick-qml自定义日历组件(支持竖屏和横屏)
Play with concurrency: draw a thread state transition diagram
QT designer plug-in implementation of QT plug-in
Shenzhen will speed up the cultivation of ecology to build a global "Hongmeng Oula city", with a maximum subsidy of 10million yuan for excellent projects
FAQ | FAQ for building applications for large screen devices
Pytorch---使用Pytorch实现U-Net进行语义分割
10 minutes to understand CMS garbage collector in JVM
Playing with concurrency: what are the ways of communication between threads?
Which product of anti-cancer insurance is better?
第十六周作业
Play with concurrency: what's the use of interruptedexception?
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
First acquaintance with P4 language
Where can I buy cancer insurance? Which product is better?
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
Monkey test
微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)