当前位置:网站首页>1123. The nearest common ancestor of the deepest leaf node
1123. The nearest common ancestor of the deepest leaf node
2022-07-07 01:20:00 【Mr Gao】
1123. The closest common ancestor of the deepest leaf node
Give you a root node root Two fork tree , Go back to it The nearest common ancestor of the deepest leaf node .
Think about it :
Leaf nodes Is a node in a binary tree that has no child nodes
Of the root node of the tree depth by 0, If the depth of a node is d, Then the depth of its child nodes is d+1
If we assume A It's a set of nodes S Of Recent public ancestor ,S Each node in the is represented by A In the subtree of the root node , And A The depth of reaches the maximum possible under this condition .
Example 1:
Input :root = [3,5,1,6,2,0,8,null,null,7,4]
Output :[2,7,4]
explain : Our return value is 2 The node of , Mark... In yellow in the figure .
What is marked in blue in the figure is the deepest node of the tree .
Be careful , node 6、0 and 8 It's also a leaf node , But their depth is 2 , And node 7 and 4 The depth of is 3 .
Example 2:
Input :root = [1]
Output :[1]
explain : The root node is the deepest node in the tree , It is its nearest common ancestor .
Example 3:
Input :root = [0,1,3,null,2]
Output :[2]
explain : The deepest leaf node in the tree is 2 , The recent public ancestor is itself .
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
#define size 100
int f(struct TreeNode* root){
if(root){
int a=f(root->left)+1;
int b=f(root->right)+1;
if(a>b){
return a;
}
return b;
}
else{
return 0;
}
}
struct TreeNode* lcaDeepestLeaves(struct TreeNode* root){
struct TreeNode *queue[size],*p;
struct TreeNode *re=NULL;
int front,rear;
int h=f(root);
front=0;
rear=0;
queue[rear]=root;
rear=(rear+1)%size;
queue[rear]=NULL;
rear=(rear+1)%size;
printf("h %d ",h);
int hz=1;
if(h==1){
re=root;
}
while(front!=rear){
p=queue[front];
front=(front+1)%size;
if(p==NULL&&front!=rear){
hz++;
queue[rear]=NULL;
rear=(rear+1)%size;
printf("hz %d ",hz);
}
else{
if(rear==front){
break;
}
printf("%d |",p->val);
if(hz==h-1){
if(p->left&&p->right){
re=p;
}
if(p->right&&p->left==NULL){
re=p->right;
}
if(p->left&&p->right==NULL){
re=p->left;
}
}
if(p->left){
queue[rear]=p->left;
rear=(rear+1)%size;
}
if(p->right){
queue[rear]=p->right;
rear=(rear+1)%size;
}
}
}
return re;
}
The following method should be better
1. Determine the depth of children around the current node , Equal, then the current node is the nearest common ancestor ;
2. If the depth of the left child is greater than that of the right child, the public ancestor must be on the left subtree ;
3. If the depth of the left child is less than that of the right child, the public ancestor must be on the right subtree ;
Traverse the tree and find a node with the same depth of the left and right subtrees .
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
int getDepth(struct TreeNode* root){
if(!root)return 0;
return fmax(getDepth(root->left),getDepth(root->right))+1;
}
struct TreeNode* lcaDeepestLeaves(struct TreeNode* root){
struct TreeNode* res=root,*cur=root;
while(cur){
int left=getDepth(cur->left);
int right=getDepth(cur->right);
if(left==right){
res=cur;
break;
}else if(left>right){
cur=cur->left;
}else cur=cur->right;
}
return res;
}
边栏推荐
- Analysis of mutex principle in golang
- Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
- 云呐|工单管理软件,工单管理软件APP
- from . cv2 import * ImportError: libGL. so. 1: cannot open shared object file: No such file or direc
- Installation and testing of pyflink
- Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
- What are the differences between Oracle Linux and CentOS?
- Can the system hibernation file be deleted? How to delete the system hibernation file
- UI control telerik UI for WinForms new theme - vs2022 heuristic theme
- Gnet: notes on the use of a lightweight and high-performance go network framework
猜你喜欢
免费白嫖的图床对比
云呐-工单管理制度及流程,工单管理规范
子网划分、构造超网 典型题
Build your own website (17)
Dark horse notes - exception handling
JTAG debugging experience of arm bare board debugging
Anfulai embedded weekly report no. 272: 2022.06.27--2022.07.03
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
UI控件Telerik UI for WinForms新主题——VS2022启发式主题
Body mass index program, entry to write dead applet project
随机推荐
golang中的atomic,以及CAS操作
docker 方法安装mysql
Spark TPCDS Data Gen
Tensorflow GPU installation
自旋与sleep的区别
Tensorflow 1.14 specify GPU running settings
Meet in the middle
[case sharing] basic function configuration of network loop detection
Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
Make Jar, Not War
Openjudge noi 1.7 10: simple password
JTAG debugging experience of arm bare board debugging
The MySQL database in Alibaba cloud was attacked, and finally the data was found
LLDP兼容CDP功能配置
Dark horse notes - exception handling
Docker method to install MySQL
子网划分、构造超网 典型题
c语言—数组
「笔记」折半搜索(Meet in the Middle)