当前位置:网站首页>二叉樹解題(二)
二叉樹解題(二)
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;
}
边栏推荐
- 微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- 【leetcode】74. Search 2D matrix
- Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
- The solution to the complexity brought by lambda expression
- 66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
- 手撕——排序
- BGP experiment the next day
- WPViewPDF Delphi 和 .NET 的 PDF 查看组件
- C language guessing numbers game
- MySQL advanced SQL statement 2
猜你喜欢
The confusion I encountered when learning stm32
【c语言】基础篇学习笔记
Hands on deep learning (II) -- multi layer perceptron
First acquaintance with string+ simple usage (II)
PIP installation of third-party libraries
Pytorch---使用Pytorch进行鸟类的预测
Introduction to vmware workstation and vSphere
Demonstration description of integrated base scheme
JVM knowledge points
pip 安装第三方库
随机推荐
Delete the code you wrote? Sentenced to 10 months!
Three ways for programmers to learn PHP easily and put chaos out of order
Force buckle 540 A single element in an ordered array
pip 安装第三方库
IDEA xml中sql没提示,且方言设置没用。
Introduction to vmware workstation and vSphere
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
Play with concurrency: what's the use of interruptedexception?
【提高课】ST表解决区间最值问题【2】
Go variables and constants
PIP installation of third-party libraries
Pytorch-Yolov5从0运行Bug解决:
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
第十六周作业
[C language] Dynamic Planning --- from entry to standing up
Www2022 | know your way back: self training method of graph neural network under distribution and migration
Deep understanding of lambda expressions
JVM knowledge points
初识P4语言