当前位置:网站首页>Binary tree problem solving (2)
Binary tree problem solving (2)
2022-07-02 04:19:00 【Hibiscus jasminoides】
Leetcode 226._ Flip binary tree
// Method 1 : Traversing the binary tree
public TreeNode InvertTree(TreeNode root)
{
// Traversing the binary tree , Swap the children of each node
Traverse(root);
return root;
}
// Binary tree traversal function
void Traverse(TreeNode root) {
if (root == null) return;
// What each node needs to do is exchange its left and right child nodes
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
// Ergodic framework , To traverse the nodes of the left and right subtrees
Traverse(root.left);
Traverse(root.right);
}
// Method 2 : Recursive function decomposition problem
public TreeNode InvertTree(TreeNode root)
{
if (root == null) return root;
// Flip left and right subtrees
TreeNode left = InvertTree(root.left);
TreeNode right = InvertTree(root.right);
// Swap left and right child nodes
root.left = right;
root.right = left;
// With root The binary tree with roots has been flipped , return root
return root;
}Leetcode 116._ Fill in the next right node pointer for each node
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;
// Put the two incoming nodes together
left.next = right;
// Connect two children of the same parent node
Traverse(left.left,left.right);
Traverse(right.left, right.right);
// Connect two child nodes that span the parent node
Traverse(left.right, right.left);
}Leetcode 114._ The binary tree is expanded into a list
public void Flatten(TreeNode root)
{
if (root == null) return;
Traverse(root);
}
void Traverse(TreeNode root)
{
if (root == null) return;
// Traverse and flatten the left and right subtrees
Traverse(root.left);
Traverse(root.right);
// Post sequence position
TreeNode left = root.left;
TreeNode right = root.right;
// Take the left subtree as the right
root.left = null;
root.right = left;
// Add the right subtree to the end of the current right subtree
TreeNode p = root;
while (p.right!=null)
{
p = p.right;
}
p.right = right;
}边栏推荐
- 66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
- Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
- Federal learning: dividing non IID samples according to Dirichlet distribution
- MySQL advanced SQL statement 2
- [JS -- map string]
- How to solve the code error when storing array data into the database
- Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
- Www 2022 | rethinking the knowledge map completion of graph convolution network
- Demonstration description of integrated base scheme
- Common locks in MySQL
猜你喜欢

Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf

First acquaintance with string+ simple usage (II)
![[C language] Dynamic Planning --- from entry to standing up](/img/7e/29482c8f3970bb1a40240e975ef97f.png)
[C language] Dynamic Planning --- from entry to standing up
![[untitled]](/img/53/cb61622cfcc73a347d2d5e852a5421.jpg)
[untitled]

藍湖的安裝及使用

The original author is out! Faker. JS has been controlled by the community..

Realizing deep learning framework from zero -- Introduction to neural network

JVM knowledge points

手撕——排序

10 minutes to understand CMS garbage collector in JVM
随机推荐
First acquaintance with P4 language
Lei Jun wrote a blog when he was a programmer. It's awesome
10 minutes to understand CMS garbage collector in JVM
The solution to the complexity brought by lambda expression
Play with concurrency: draw a thread state transition diagram
go 变量与常量
go 语言命名规范
Hands on deep learning (II) -- multi layer perceptron
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
QT designer plug-in implementation of QT plug-in
蓝湖的安装及使用
go 分支与循环
PR zero foundation introductory guide note 2
【leetcode】34. Find the first and last positions of elements in a sorted array
Actual combat | use composite material 3 in application
千亿市场规模医疗美容行业的水究竟有多浑?
Introduction to JSON usage scenarios and precautions
pip 安装第三方库
[C language] basic learning notes