当前位置:网站首页>二叉樹解題(二)
二叉樹解題(二)
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;
}边栏推荐
- Pytoch --- use pytoch to predict birds
- Hands on deep learning (II) -- multi layer perceptron
- Li Kou interview question 02.08 Loop detection
- Go function
- 二叉树解题(二)
- Pytorch---使用Pytorch进行鸟类的预测
- 66.qt quick-qml自定义日历组件(支持竖屏和横屏)
- 整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
- Websites that it people often visit
- C language practice - binary search (half search)
猜你喜欢

office_ Delete the last page of word (the seemingly blank page)

How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of

手撕——排序

Sorted out an ECS summer money saving secret, this time @ old users come and take it away

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
![[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)](/img/e1/620443dbc6ea8b326e1242f25d6d74.jpg)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)

Play with concurrency: draw a thread state transition diagram

How much can a job hopping increase? Today, I saw the ceiling of job hopping.

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

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
随机推荐
"No war on the Western Front" we just began to love life, but we had to shoot at everything
The solution to the complexity brought by lambda expression
Pytoch --- use pytoch to predict birds
okcc为什么云呼叫中心比传统呼叫中心更好?
Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
Target free or target specific: a simple and effective zero sample position detection comparative learning method
Fluent icon demo
【毕业季·进击的技术er】年少有梦,何惧彷徨
Playing with concurrency: what are the ways of communication between threads?
[C language] Dynamic Planning --- from entry to standing up
Uni app - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
CorelDRAW Graphics Suite2022免费图形设计软件
First acquaintance with string+ simple usage (II)
初识P4语言
[untitled]
PR zero foundation introductory guide note 2
二叉树解题(二)
66.qt quick-qml自定义日历组件(支持竖屏和横屏)
Is the product of cancer prevention medical insurance safe?