当前位置:网站首页>Notes on writing questions (18) -- binary tree: common ancestor problem
Notes on writing questions (18) -- binary tree: common ancestor problem
2022-06-24 21:59:00 【Dream of becoming a skinhead!】
Catalog
List of articles
Brush notes ( One )– An array type : Dichotomy
Brush notes ( Two )– An array type : Double finger needling
Brush notes ( 3、 ... and )– An array type : The sliding window
Brush notes ( Four )– An array type : simulation
Brush notes ( 5、 ... and )– List type : Basic topics and operations
Brush notes ( 6、 ... and )– Hashtable : Basic topics and ideas
Brush notes ( 7、 ... and )– character string : Classic title
Brush notes ( 8、 ... and )– Double pointer : Sum of two numbers and extension
Brush notes ( Nine )– character string :KMP Algorithm
Brush notes ( Ten )– Stacks and queues : Basic topics
Brush notes ( 11、 ... and )– Stacks and queues :Top-K problem
Brush notes ( Twelve )– review : Sorting algorithm
Brush notes ( 13、 ... and )– Binary tree : Traversal in front, middle and back order ( review )
Brush notes ( fourteen )– Binary tree : Sequence traversal and DFS,BFS
Brush notes ( 15、 ... and )– Binary tree : Attribute related topics
Brush notes ( sixteen )– Binary tree : Modification and construction
Brush notes ( seventeen )– Binary search tree : About attributes
Preface
The binary tree is almost over , come on. !!!
Bibliography
236. The nearest common ancestor of a binary tree
The title links are as follows :
236. The nearest common ancestor of a binary tree
The screenshot of the title is as follows :

How to do this problem ? In fact, it should be carried out step by step
Step one
Give the root node , And target nodes , Then check whether the target node is a subtree of the root node . This is very simple, not ?
public boolean find(TreeNode root,TreeNode target){
if(root == null) return false;
if(root == target) return true;
return find(root.left,target) || find(root.right,target);
}
Step two
Then it must be discussed according to the situation
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// The next step is to make a judgment
if(root == null) return null;
if(root == p || root == q) return root;// if p perhaps q Any one is the root node , The ancestor must be the root node
// if p q The nodes are on the left , Then go to Zuozi tree to find your ancestors
if(find(root.left,p) && find(root.left,q)) return lowestCommonAncestor(root.left,p,q);
// if p q The nodes are on the right , Then go to Youzi tree to find your ancestors
if(find(root.right,p) && find(root.right,q)) return lowestCommonAncestor(root.right,p,q);
// If not on one side at the same time , The current node must be the ancestor
return root;
}
Step three
What's the third step ? It must be simplification , It must be possible to write like that , But it's a little long . Code first , Then explain the code
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q) return root;// This step is the key
TreeNode left = lowestCommonAncestor(root.left,p,q);
TreeNode right = lowestCommonAncestor(root.right,p,q);
if(left == null && right == null) return null;
if(left == null) return right;
if(right == null) return left;
return root;
}
This code is to merge step one and step , But here …emmm, The point is : After the sequence traversal . Same as above , Look at it in two steps
Step one :
That's the first step , Is a recursive process , Then look for p and q node .

This is the second step , It's about judging what you're looking for .
These two steps are the decomposition of each layer of recursion , It seems a bit chaotic, isn't it ??emmm, Stop for a second , In the future, if I can organize my own language, I will continue .
235. The nearest common ancestor of a binary search tree
Topic link :
235. The nearest common ancestor of a binary search tree
Title screenshot :

In fact, it can be treated as an ordinary tree , But since the binary search tree is mentioned here , It's the same as the last question , Even the code doesn't have to change .
But what? , Since the binary search tree is mentioned here , Then the current time complexity can be optimized by the properties of binary search tree .
public class The common ancestor of binary search tree {
// The common ancestor of a binary tree is the postorder traversal , I can also follow up here ? My first thought is the preorder traversal
// Preorder traversal is still a little problematic ,return root repeated
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root.val > p.val && root.val > q.val){
return lowestCommonAncestor(root.left,p,q);
}
if(root.val < p.val && root.val < q.val){
return lowestCommonAncestor(root.right,p,q);
}
return root;
}
}
边栏推荐
- 机器学习:线性回归
- CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)
- C语言-关键字1
- Elegant custom ThreadPoolExecutor thread pool
- Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
- EasyBypass
- Machine learning: linear regression
- Vscode netless environment rapid migration development environment (VIP collection version)
- leetcode1863_ 2021-10-14
- Visit Amazon memorydb and build your own redis memory database
猜你喜欢

是真干不过00后,给我卷的崩溃,想离职了...
![[featured] how do you design unified login with multiple accounts?](/img/df/9b4fc11a6971ebe8162ae84250a782.png)
[featured] how do you design unified login with multiple accounts?

平衡二叉搜索树

EasyBypass

You are using pip version 21.1.2; however, version 22.1.2 is available

socket(1)

传输层 udp && tcp

直击“三夏”生产:丰收喜报频传 夏播紧锣密鼓

Kubernetes 集群中流量暴露的几种方案

Xinlou: Huawei's seven-year building journey of sports health
随机推荐
多线程收尾
leetcode1720_ 2021-10-14
Data link layer & some other protocols or technologies
最大流问题
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
【吴恩达笔记】机器学习基础
【吴恩达笔记】卷积神经网络
2022 international women engineers' Day: Dyson design award shows women's design strength
Prompt that the device has no permission when using ADB to connect to the device
CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)
Multithreaded finalization
[untitled]
Machine learning: gradient descent method
二叉搜索树模板
socket done
LeetCode-513. 找树左下角的值
Kubernetes 集群中流量暴露的几种方案
St Table + two points
即构「畅直播」上线!提供全链路升级的一站式直播服务