当前位置:网站首页>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;
}
边栏推荐
- Thinkphp6 limit interface access frequency
- go 分支与循环
- [JS -- map string]
- Playing with concurrency: what are the ways of communication between threads?
- Pytoch --- use pytoch to realize u-net semantic segmentation
- Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
- Fingertips life Chapter 4 modules and packages
- 二叉树解题(二)
- Wechat applet pull-down loading more waterfall flow loading
- Play with concurrency: draw a thread state transition diagram
猜你喜欢
QT designer plug-in implementation of QT plug-in
Use a mask to restrict the input of the qlineedit control
Installation et utilisation du lac bleu
Déchirure à la main - tri
Common sense of cloud server security settings
Free drawing software recommended - draw io
10 minutes to understand CMS garbage collector in JVM
Raspberry pie GPIO pin controls traffic light and buzzer
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
随机推荐
The confusion I encountered when learning stm32
Fingertips life Chapter 4 modules and packages
Opencv learning example code 3.2.4 LUT
[C language] basic learning notes
C language guessing numbers game
Document declaration and character encoding
第十六周作业
Spring moves are coming. Watch the gods fight
cookie、session、tooken
C语言:逻辑运算和判断选择结构例题
First acquaintance with P4 language
CorelDRAW Graphics Suite2022免费图形设计软件
Feature Engineering: summary of common feature transformation methods
[untitled]
unable to execute xxx. SH: operation not permitted
pip 安装第三方库
C language: examples of logical operation and judgment selection structure
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
Uni app - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
10 minutes to understand CMS garbage collector in JVM