当前位置:网站首页>二叉树解题(二)
二叉树解题(二)
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 code error when storing array data into the database
- [wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
- First acquaintance with string+ simple usage (II)
- The confusion I encountered when learning stm32
- PR zero foundation introductory guide note 2
- Document declaration and character encoding
- 第十六周作业
- Mysql中常见的锁
- Use a mask to restrict the input of the qlineedit control
- Typescript practice for SAP ui5
猜你喜欢
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Www 2022 | rethinking the knowledge map completion of graph convolution network
BiShe cinema ticket purchasing system based on SSM
Target free or target specific: a simple and effective zero sample position detection comparative learning method
PIP installation of third-party libraries
Delete the code you wrote? Sentenced to 10 months!
Déchirure à la main - tri
手撕——排序
[C language] basic learning notes
随机推荐
Where can I buy cancer insurance? Which product is better?
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
藍湖的安裝及使用
okcc为什么云呼叫中心比传统呼叫中心更好?
LCM of Spreadtrum platform rotates 180 °
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
[JS event -- event flow]
Li Kou interview question 02.08 Loop detection
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Fluent icon demo
【提高课】ST表解决区间最值问题【2】
Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations
[untitled]
go 函数
WiFi 5GHz frequency
Document declaration and character encoding
【c语言】基础篇学习笔记
Feature Engineering: summary of common feature transformation methods
Today's plan: February 15, 2022