当前位置:网站首页>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;
}
边栏推荐
- 黑马笔记---异常处理
- Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
- NEON优化:性能优化经验总结
- Niuke cold training camp 6B (Freund has no green name level)
- Atomic in golang, and cas Operations
- Anfulai embedded weekly report no. 272: 2022.06.27--2022.07.03
- Openjudge noi 1.7 08: character substitution
- The difference between spin and sleep
- 子网划分、构造超网 典型题
- Supersocket 1.6 creates a simple socket server with message length in the header
猜你喜欢

资产安全问题或制约加密行业发展 风控+合规成为平台破局关键

黑马笔记---创建不可变集合与Stream流

Do you understand this patch of the interface control devaxpress WinForms skin editor?

【信号与系统】

动态规划思想《从入门到放弃》

界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?

HMM 笔记

JTAG principle of arm bare board debugging
![[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)](/img/40/dc45df3cd3ee7642277eff899bc6aa.png)
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)

云呐|工单管理办法,如何开展工单管理
随机推荐
Taro2.* applet configuration sharing wechat circle of friends
Pytorch中torch和torchvision的安装
Cause of handler memory leak
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
Tensorflow 1.14 specify GPU running settings
Neon Optimization: summary of performance optimization experience
Dynamic planning idea "from getting started to giving up"
NEON优化:矩阵转置的指令优化案例
第三方跳转网站 出现 405 Method Not Allowed
Vocabulary in Data Book
【信号与系统】
Installation of torch and torch vision in pytorch
Build your own website (17)
云呐|工单管理软件,工单管理软件APP
THREE.AxesHelper is not a constructor
golang中的atomic,以及CAS操作
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
Receive user input, height BMI, BMI detection small business entry case
黑马笔记---创建不可变集合与Stream流
接收用户输入,身高BMI体重指数检测小业务入门案例