当前位置:网站首页>236. The nearest common ancestor of a binary tree (medium)
236. The nearest common ancestor of a binary tree (medium)
2022-04-22 08:40:00 【weixin_ forty-six million two hundred and seventy-two thousand 】
236. The nearest common ancestor of a binary tree
Given a binary tree , Find the nearest common ancestor of the two specified nodes in the tree .
In Baidu Encyclopedia, the most recent definition of public ancestor is :“ For a tree T Two nodes of p、q, The nearest common ancestor is represented as a node x, Satisfy x yes p、q Our ancestors and x As deep as possible ( A node can also be its own ancestor ).”

Example 1:
Input :root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output :3
explain : node 5 And nodes 1 The most recent public ancestor of is the node 3 .
Ideas (dfs)
Think about what is a common ancestor node
- The left and right subtrees of this node each contain the target node
- The node itself is a target node , The subtree in one direction contains another target node
if ((left && right) || ((root->val == p->val || root->val == q->val) && (left || right))) {
ancestor = root;
}
Then the above is the judgment condition , We use it bool Value to mark the situation of each child node
bool left = dfs(root->left, p, q);// Recursive left subtree , Judge its true value
bool right = dfs(root->right, p, q);// Recursive right subtree , Judge its true value
Recursion from bottom to top , Of the child nodes of the upper layer bool The value is determined by the node of the next layer

The picture comes from the official explanation
https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/
Source code
class Solution {
public:
TreeNode* ancestor;
bool dfs(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == nullptr) {
return false;
}
bool left = dfs(root->left, p, q);// Recursive left subtree , Judge its true value
bool right = dfs(root->right, p, q);// Recursive right subtree , Judge its true value
if ( (left && right) || ((root->val == p->val || root->val == q->val) && (left || right))) {
// If the node has its target nodes on both sides , Or change the node itself to the target node , Its subtree in one direction contains another node , Then change the node to the nearest ancestor node
ancestor = root;// Because it's from bottom to top , So the depth of the first discovery is the deepest , For the nearest ancestor node
}
return left || right || (root->val == p->val || root->val == q->val);// Return the true value according to the situation
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
dfs(root,p,q);
return ancestor;
}
};
版权声明
本文为[weixin_ forty-six million two hundred and seventy-two thousand ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220742290904.html
边栏推荐
猜你喜欢

SQL 語句中 “意想不到” 的操作

WeChat official account - webpage authorization

Detailed analysis of viewpager usage
![[paper reading] [3D target detection] pvgnet](/img/fc/7fd22bbc1aaf4bc5ae38543cd03ae3.png)
[paper reading] [3D target detection] pvgnet

Algorithm -- delete the penultimate node of the linked list (kotlin)

shell脚本学习笔记——正则表达式

CentOS 安裝 MySQL

Teach you how to realize the pull-down refresh and pull-up load of recyclerview

华为机试题——HJ53 杨辉三角的变形

Machine learning notes - determinant
随机推荐
SQL database multiple choice question (1)
94. Middle order traversal of binary tree (easy)
C的动态内存管理
函数指针和指针函数
OLED display driver
golang 环境搭建
Use the method of onlayoutchangelistener to solve the problem of gettop = 0
[no very low code] low code platform development diary, SQL programming of low code platform
235. 二叉搜索树的最近公共祖先(Easy)
秋招求职总结分享
Hollow letter pyramid
Machine learning notes - determinant
Lambda 表达式
第1关:继承
redis 简单使用
MaterialApp
Flutter Foundation
MFC demo of data encoding
Nacos Foundation (3): open API configuration management test and close Nacos service
字符串的替换