当前位置:网站首页>二叉樹解題(二)
二叉樹解題(二)
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;
}
边栏推荐
- Yyds dry inventory compiler and compiler tools
- First acquaintance with P4 language
- Major domestic quantitative trading platforms
- Qt插件之Qt Designer插件实现
- 手撕——排序
- LxC limits the number of CPUs
- There is no prompt for SQL in idea XML, and the dialect setting is useless.
- [source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
- Installation et utilisation du lac bleu
- Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
猜你喜欢
Federal learning: dividing non IID samples according to Dirichlet distribution
[untitled]
Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Pytorch---使用Pytorch实现U-Net进行语义分割
JVM knowledge points
MySQL advanced SQL statement 2
云服务器的安全设置常识
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
Qt插件之Qt Designer插件实现
随机推荐
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
Installation et utilisation du lac bleu
Wechat applet map annotation
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Typescript practice for SAP ui5
云服务器的安全设置常识
[untitled]
[C language] basic learning notes
go 语言命名规范
go 包的使用
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
Use a mask to restrict the input of the qlineedit control
pip 安装第三方库
First acquaintance with P4 language
Realizing deep learning framework from zero -- Introduction to neural network
First acquaintance with string+ simple usage (II)
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
Wechat applet pull-down loading more waterfall flow loading
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
The confusion I encountered when learning stm32