当前位置:网站首页>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;
}
边栏推荐
- [JS event -- event flow]
- [C language] Dynamic Planning --- from entry to standing up
- Spring moves are coming. Watch the gods fight
- [JS -- map string]
- Playing with concurrency: what are the ways of communication between threads?
- cookie、session、tooken
- 云服务器的安全设置常识
- The original author is out! Faker. JS has been controlled by the community..
- PR zero foundation introductory guide note 2
- Lei Jun wrote a blog when he was a programmer. It's awesome
猜你喜欢
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
【c语言】基础篇学习笔记
Pytorch---使用Pytorch进行鸟类的预测
"No war on the Western Front" we just began to love life, but we had to shoot at everything
First acquaintance with string+ simple usage (II)
Installation and use of blue lake
Target free or target specific: a simple and effective zero sample position detection comparative learning method
Play with concurrency: draw a thread state transition diagram
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
Free drawing software recommended - draw io
随机推荐
Fluent icon demo
Unit testing classic three questions: what, why, and how?
Qt插件之Qt Designer插件实现
cookie、session、tooken
【leetcode】74. Search 2D matrix
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Pytorch-Yolov5從0運行Bug解决:
Actual combat | use composite material 3 in application
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
Demonstration description of integrated base scheme
Go branch and loop
【毕业季·进击的技术er】年少有梦,何惧彷徨
The confusion I encountered when learning stm32
手撕——排序
Wechat applet pull-down loading more waterfall flow loading
《动手学深度学习》(二)-- 多层感知机
Play with concurrency: draw a thread state transition diagram
Play with concurrency: what's the use of interruptedexception?
Wpviewpdf Delphi and Net PDF viewing component
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