当前位置:网站首页>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;
}边栏推荐
- 【毕业季·进击的技术er】年少有梦,何惧彷徨
- Free drawing software recommended - draw io
- Play with concurrency: what's the use of interruptedexception?
- Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
- Read "the way to clean code" - function names should express their behavior
- 蓝湖的安装及使用
- Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
- Go language naming specification
- Wechat applet calculates the distance between the two places
- C language practice - binary search (half search)
猜你喜欢

Pytoch --- use pytoch to realize u-net semantic segmentation

Installation and use of blue lake

FAQ | FAQ for building applications for large screen devices

Go language introduction

Pytorch---使用Pytorch实现U-Net进行语义分割

Wpviewpdf Delphi and Net PDF viewing component
![[JS event -- event flow]](/img/fe/199890b082845f68b65f25056e6f29.jpg)
[JS event -- event flow]

Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000

The solution to the complexity brought by lambda expression

How much can a job hopping increase? Today, I saw the ceiling of job hopping.
随机推荐
【c语言】基础篇学习笔记
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Yyds dry inventory compiler and compiler tools
Qt插件之Qt Designer插件实现
Installation et utilisation du lac bleu
[untitled]
Where can I buy cancer insurance? Which product is better?
Pytoch --- use pytoch for image positioning
深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
FAQ | FAQ for building applications for large screen devices
Websites that it people often visit
How to solve the code error when storing array data into the database
go 变量与常量
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
WiFi 5GHz frequency
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
【c语言】动态规划---入门到起立
Force buckle 540 A single element in an ordered array