当前位置:网站首页>二叉樹解題(二)
二叉樹解題(二)
2022-07-02 04:18:00 【木蘇栀槿】
Leetcode 226._翻轉二叉樹
// 方法一:遍曆二叉樹
public TreeNode InvertTree(TreeNode root)
{
// 遍曆二叉樹,交換每個節點的子節點
Traverse(root);
return root;
}
// 二叉樹遍曆函數
void Traverse(TreeNode root) {
if (root == null) return;
// 每一個節點需要做的事就是交換它的左右子節點
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
// 遍曆框架,去遍曆左右子樹的節點
Traverse(root.left);
Traverse(root.right);
}
// 方法二:遞歸函數分解問題
public TreeNode InvertTree(TreeNode root)
{
if (root == null) return root;
// 翻轉左右子樹
TreeNode left = InvertTree(root.left);
TreeNode right = InvertTree(root.right);
// 交換左右子節點
root.left = right;
root.right = left;
// 以 root 為根的這棵二叉樹已經被翻轉,返回 root
return root;
}Leetcode 116._填充每個節點的下一個右側節點指針
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;
// 將傳入的兩個節點穿起來
left.next = right;
// 連接相同父節點的兩個子節點
Traverse(left.left,left.right);
Traverse(right.left, right.right);
// 連接跨越父節點的兩個子節點
Traverse(left.right, right.left);
}Leetcode 114._二叉樹展開為鏈錶
public void Flatten(TreeNode root)
{
if (root == null) return;
Traverse(root);
}
void Traverse(TreeNode root)
{
if (root == null) return;
// 遍曆拉平左右子樹
Traverse(root.left);
Traverse(root.right);
// 後序比特置
TreeNode left = root.left;
TreeNode right = root.right;
// 將左子樹作為右子樹
root.left = null;
root.right = left;
// 將右子樹添加到當前右子樹末尾
TreeNode p = root;
while (p.right!=null)
{
p = p.right;
}
p.right = right;
}边栏推荐
- Pytorch---使用Pytorch进行鸟类的预测
- C language: examples of logical operation and judgment selection structure
- Pytoch --- use pytoch for image positioning
- unable to execute xxx. SH: operation not permitted
- Bitmap principle code record
- powershell_ View PowerShell function source code (environment variable / alias) / take function as parameter
- How much is the tuition fee of SCM training class? How long is the study time?
- LxC limits the number of CPUs
- Pytoch --- use pytoch to predict birds
- 10 minutes to understand CMS garbage collector in JVM
猜你喜欢

First acquaintance with string+ simple usage (II)

Pytoch --- use pytoch to predict birds

Go language introduction

Force buckle 540 A single element in an ordered array

【c语言】基础篇学习笔记

初识P4语言
![[untitled]](/img/53/cb61622cfcc73a347d2d5e852a5421.jpg)
[untitled]

Pytorch---使用Pytorch进行图像定位

手撕——排序

Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
随机推荐
IDEA xml中sql没提示,且方言设置没用。
文档声明与字符编码
Actual combat | use composite material 3 in application
JVM knowledge points
Is the product of cancer prevention medical insurance safe?
Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
初识P4语言
Pytoch --- use pytoch to realize u-net semantic segmentation
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
cookie、session、tooken
The original author is out! Faker. JS has been controlled by the community..
[C language] basic learning notes
Pytoch --- use pytoch for image positioning
Wechat applet JWT login issue token
Introduction to vmware workstation and vSphere
Go branch and loop
How to solve the problem that objects cannot be deleted in Editor Mode
[untitled]
Federal learning: dividing non IID samples according to Dirichlet distribution
Which insurance company has a better product of anti-cancer insurance?