当前位置:网站首页>二叉树解题(二)
二叉树解题(二)
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;
}边栏推荐
- Www2022 | know your way back: self training method of graph neural network under distribution and migration
- Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
- Qt插件之Qt Designer插件实现
- LxC limits the number of CPUs
- Spring moves are coming. Watch the gods fight
- IDEA xml中sql没提示,且方言设置没用。
- WPViewPDF Delphi 和 .NET 的 PDF 查看组件
- How much can a job hopping increase? Today, I saw the ceiling of job hopping.
- 微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- 【毕业季·进击的技术er】年少有梦,何惧彷徨
猜你喜欢

First acquaintance with P4 language

文档声明与字符编码

Fluent icon demo

Use of go package

手撕——排序

What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?

【c语言】动态规划---入门到起立

Typescript practice for SAP ui5

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

BiShe cinema ticket purchasing system based on SSM
随机推荐
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
Wechat applet JWT login issue token
Realizing deep learning framework from zero -- Introduction to neural network
There is no prompt for SQL in idea XML, and the dialect setting is useless.
5g era is coming in an all-round way, talking about the past and present life of mobile communication
Lei Jun wrote a blog when he was a programmer. It's awesome
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
Message mechanism -- message processing
Qt插件之Qt Designer插件实现
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
Introduction to JSON usage scenarios and precautions
C语言:逻辑运算和判断选择结构例题
手撕——排序
Typescript practice for SAP ui5
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
Playing with concurrency: what are the ways of communication between threads?
SQL: common SQL commands
How much is the tuition fee of SCM training class? How long is the study time?
【leetcode】34. Find the first and last positions of elements in a sorted array
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map