当前位置:网站首页>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;
}
边栏推荐
- Bitmap principle code record
- C language practice - number guessing game
- pip 安装第三方库
- 手撕——排序
- How to solve the code error when storing array data into the database
- BGP experiment the next day
- Which insurance company has a better product of anti-cancer insurance?
- Okcc why is cloud call center better than traditional call center?
- Wechat applet JWT login issue token
- Raspberry pie GPIO pin controls traffic light and buzzer
猜你喜欢
Www2022 | know your way back: self training method of graph neural network under distribution and migration
Play with concurrency: draw a thread state transition diagram
Websites that it people often visit
【leetcode】34. Find the first and last positions of elements in a sorted array
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
阿里云polkit pkexec 本地提权漏洞
FAQ | FAQ for building applications for large screen devices
Lei Jun wrote a blog when he was a programmer. It's awesome
手撕——排序
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
随机推荐
Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
Landing guide for "prohibit using select * as query field list"
Three ways for programmers to learn PHP easily and put chaos out of order
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
【提高课】ST表解决区间最值问题【2】
go 包的使用
Okcc why is cloud call center better than traditional call center?
Demonstration description of integrated base scheme
The solution to the complexity brought by lambda expression
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
Pytorch---使用Pytorch进行鸟类的预测
Dare to go out for an interview without learning some distributed technology?
Monkey测试
【leetcode】81. Search rotation sort array II
Deep understanding of lambda expressions
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
Shutdown procedure after 60
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Pytorch-Yolov5從0運行Bug解决: