当前位置:网站首页>Binary tree problem solving (1)
Binary tree problem solving (1)
2022-07-02 04:19:00 【Hibiscus jasminoides】
First understand the traversal of the first, middle and second order of binary tree :
The former sequence traversal = Left subtree preorder traversal + Right subtree preorder traversal
Therefore, the middle sequence and the rear sequence only need to res.Add(root.val); Put it in the corresponding position ;
public IList<int> Traversal(TreeNode root){
List<int> res = new List<int>();
if (root == null)
return res;
// Preamble position res.Add(root.val);
left = Traversal(root.left);
// Middle order position res.Add(root.val);
right = Traversal(root.right);
// Post sequence position res.Add(root.val);
return res;
}The preamble position is executed when the code enters the node ;
The post sequence position is executed when the code leaves the node ;
The middle order position is after the code traverses the left subtree , Execute before traversing the right subtree ;
You can do what you need at these three special time points .
You just need to think about what each node should do , Don't worry about the others , Throw it to the binary tree traversal framework , Recursion will do the same for all nodes .
The general thinking process when encountering the problem of a binary tree is : Whether you can get the answer by traversing the binary tree ? If not , Whether a recursive function can be defined , Pass the subproblem ( subtree ) The answer to the original question ? If you need to design subtree information , Follow up traversal is recommended
Leetcode 617. Merge binary tree -- Simple
public TreeNode MergeTrees(TreeNode root1, TreeNode root2)
{
// No tree , Next to another tree ,
if (root1 == null)
return root2;
if (root2 == null)
return root1;
// Merge values at the same location
root1.val +=root2.val;
// Recursively merge left and right subtrees
root1.left = MergeTrees(root1.left, root2.left);
root1.right = MergeTrees(root1.right, root2.right);
return root1;
}Leetcode 669. Prune the binary search tree -- secondary
public TreeNode TrimBST(TreeNode root, int low, int high) {
if (root==null) return null;
// Delete left (root and root.left) All nodes , return root.right
if (root.val<low)
{
return TrimBST(root.right, low, high);
}
// Delete the right side (root and root.right) All nodes , return root.left
if (root.val > high)
{
return TrimBST(root.left, low, high);
}
// stay [low,hight] No processing in the interval
root.left = TrimBST(root.left, low, high);
root.right = TrimBST(root.right, low, high);
return root;
}Leetcode 124. The maximum path in a binary tree and -- difficult
int res = int.MinValue;
public int MaxPathSum(TreeNode root)
{
if (root == null) return 0;
Traverse(root);
return res;
}
int Traverse(TreeNode root)
{
if (root == null) return 0;
int left = Math.Max(0, Traverse(root.left));
int right = Math.Max(0, Traverse(root.right));
// Post sequence position , Update the maximum path and
res = Math.Max(res, root.val + left + right);
// From the root node root Is the maximum unilateral path and
return Math.Max(left,right)+root.val;
}边栏推荐
- Monkey test
- Read "the way to clean code" - function names should express their behavior
- C language: examples of logical operation and judgment selection structure
- Opencv learning example code 3.2.4 LUT
- Go language naming specification
- Use a mask to restrict the input of the qlineedit control
- Li Kou interview question 02.08 Loop detection
- Which product of anti-cancer insurance is better?
- Demonstration description of integrated base scheme
- Go function
猜你喜欢

Read "the way to clean code" - function names should express their behavior

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

Landing guide for "prohibit using select * as query field list"

Websites that it people often visit

《动手学深度学习》(二)-- 多层感知机

What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?

文档声明与字符编码

Deep understanding of lambda expressions

Fluent icon demo

Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
随机推荐
go 分支与循环
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
Read "the way to clean code" - function names should express their behavior
10 minutes to understand CMS garbage collector in JVM
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
Document declaration and character encoding
【提高课】ST表解决区间最值问题【2】
LxC limits the number of CPUs
Wechat applet pull-down loading more waterfall flow loading
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 can a job hopping increase? Today, I saw the ceiling of job hopping.
Deep understanding of lambda expressions
Today's plan: February 15, 2022
Wechat applet JWT login issue token
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
【毕业季·进击的技术er】年少有梦,何惧彷徨
【c语言】基础篇学习笔记
初识P4语言
Actual combat | use composite material 3 in application
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent