当前位置:网站首页>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 solve the problem that objects cannot be deleted in Editor Mode
- office_ Delete the last page of word (the seemingly blank page)
- 【leetcode】34. Find the first and last positions of elements in a sorted array
- Pytorch-Yolov5从0运行Bug解决:
- Go function
- Common locks in MySQL
- Pytorch-Yolov5從0運行Bug解决:
- Pytorch---使用Pytorch进行图像定位
- Raspberry pie GPIO pin controls traffic light and buzzer
- CorelDRAW Graphics Suite2022免费图形设计软件
猜你喜欢
Realizing deep learning framework from zero -- Introduction to neural network
初识P4语言
【leetcode】34. Find the first and last positions of elements in a sorted array
Demonstration description of integrated base scheme
Pytoch --- use pytoch for image positioning
Pytoch --- use pytoch to realize u-net semantic segmentation
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
66.qt quick-qml自定义日历组件(支持竖屏和横屏)
Unit testing classic three questions: what, why, and how?
Playing with concurrency: what are the ways of communication between threads?
随机推荐
C language practice - number guessing game
MySQL advanced SQL statement 2
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
unable to execute xxx. SH: operation not permitted
千亿市场规模医疗美容行业的水究竟有多浑?
Lei Jun wrote a blog when he was a programmer. It's awesome
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
How to solve the problem that objects cannot be deleted in Editor Mode
Three ways for programmers to learn PHP easily and put chaos out of order
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
【提高课】ST表解决区间最值问题【2】
Installation et utilisation du lac bleu
Which insurance company has a better product of anti-cancer insurance?
Feature Engineering: summary of common feature transformation methods
Go language naming specification
Pytorch-Yolov5從0運行Bug解决:
pip 安装第三方库
【c语言】基础篇学习笔记
Force buckle 540 A single element in an ordered array