当前位置:网站首页>二叉树解题(二)
二叉树解题(二)
2022-07-02 04:17: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;
}边栏推荐
- How to solve the problem that objects cannot be deleted in Editor Mode
- Pytorch---使用Pytorch进行图像定位
- Use a mask to restrict the input of the qlineedit control
- Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
- 云服务器的安全设置常识
- Pytorch-Yolov5从0运行Bug解决:
- powershell_ View PowerShell function source code (environment variable / alias) / take function as parameter
- 初识P4语言
- Common locks in MySQL
- Target free or target specific: a simple and effective zero sample position detection comparative learning method
猜你喜欢

手撕——排序

MySQL advanced SQL statement 2

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

The original author is out! Faker. JS has been controlled by the community..

Hands on deep learning (II) -- multi layer perceptron

Www 2022 | rethinking the knowledge map completion of graph convolution network

文档声明与字符编码

Dare to go out for an interview without learning some distributed technology?

整理了一份ECS夏日省钱秘籍,这次@老用户快来领走

Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
随机推荐
Pytoch --- use pytoch for image positioning
《动手学深度学习》(二)-- 多层感知机
Wechat applet JWT login issue token
Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
PIP installation of third-party libraries
阿里云polkit pkexec 本地提权漏洞
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
Wpviewpdf Delphi and Net PDF viewing component
文档声明与字符编码
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
【c语言】动态规划---入门到起立
Wechat applet pull-down loading more waterfall flow loading
Okcc why is cloud call center better than traditional call center?
The confusion I encountered when learning stm32
Déchirure à la main - tri
Websites that it people often visit