当前位置:网站首页>二叉樹解題(二)
二叉樹解題(二)
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;
}边栏推荐
- WiFi 5GHz frequency
- Www 2022 | rethinking the knowledge map completion of graph convolution network
- 60后关机程序
- Typescript practice for SAP ui5
- Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
- Three ways for programmers to learn PHP easily and put chaos out of order
- Major domestic quantitative trading platforms
- MySQL advanced SQL statement 2
- C language practice - binary search (half search)
- SQL:常用的 SQL 命令
猜你喜欢

A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent

Www2022 | know your way back: self training method of graph neural network under distribution and migration

10 minutes to understand CMS garbage collector in JVM

66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)

蓝湖的安装及使用

unable to execute xxx. SH: operation not permitted

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

Go语言介绍

66.qt quick-qml自定义日历组件(支持竖屏和横屏)

First acquaintance with string+ simple usage (II)
随机推荐
Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
The solution to the complexity brought by lambda expression
Www 2022 | rethinking the knowledge map completion of graph convolution network
Common locks in MySQL
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
C language practice - binary search (half search)
Raspberry pie GPIO pin controls traffic light and buzzer
Spring moves are coming. Watch the gods fight
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
How much is the tuition fee of SCM training class? How long is the study time?
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Go language introduction
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
What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
手撕——排序
go 分支与循环
【毕业季·进击的技术er】年少有梦,何惧彷徨
[untitled]
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
Fluent icon demo