当前位置:网站首页>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;
}
边栏推荐
- 云呐|工单管理软件,工单管理软件APP
- boot - prometheus-push gateway 使用
- mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
- Case development of landlord fighting game
- Using the entry level of DVA in taro3.*
- Deep learning framework TF installation
- Gnet: notes on the use of a lightweight and high-performance go network framework
- Receive user input, height BMI, BMI detection small business entry case
- NEON优化:性能优化常见问题QA
- 让我们,从头到尾,通透网络I/O模型
猜你喜欢

pytorch之数据类型tensor

第三方跳转网站 出现 405 Method Not Allowed

Build your own website (17)

The MySQL database in Alibaba cloud was attacked, and finally the data was found

Transformation transformation operator

Maidong Internet won the bid of Beijing life insurance to boost customers' brand value

Anfulai embedded weekly report no. 272: 2022.06.27--2022.07.03

Gazebo的安装&与ROS的连接

Dark horse notes - create immutable sets and streams

Dark horse notes - exception handling
随机推荐
Docker method to install MySQL
Dark horse notes - create immutable sets and streams
2022 Google CTF SEGFAULT LABYRINTH wp
from . cv2 import * ImportError: libGL. so. 1: cannot open shared object file: No such file or direc
【芯片方案设计】脉搏血氧仪
c语言—数组
7.6模拟赛总结
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
NEON优化:矩阵转置的指令优化案例
Using the entry level of DVA in taro3.*
从零开始匹配vim(0)——vimscript 简介
Neon Optimization: an optimization case of log10 function
C language - array
Data type of pytorch tensor
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
Dynamic planning idea "from getting started to giving up"
taro3.*中使用 dva 入门级别的哦
SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
【案例分享】网络环路检测基本功能配置
golang中的Mutex原理解析