首页 > 其他分享 >LeetCode: 236. Lowest Common Ancestor of a Binary Tree

LeetCode: 236. Lowest Common Ancestor of a Binary Tree

时间:2022-12-05 18:01:54浏览次数:63  
标签:Lowest Binary lowestCommonAncestor TreeNode nodesInTree Tree LCA nodes root


LeetCode: 236. Lowest Common Ancestor of a Binary Tree

题目描述

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the ​​definition of LCA on Wikipedia​​: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Given the following binary tree: 1root = [3,5,1,6,2,0,8,null,null,7,4]1

LeetCode: 236. Lowest Common Ancestor of a Binary Tree_递归


Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5,
since a node can be a descendant of itself according to the LCA definition.

Note:

  • All of the nodes’ values will be unique.
  • ​p​​​ and ​​q​​ are different and both values will exist in the binary tree.

解题思路 —— 递归求解

根据左右子树含有待求公共祖先节点的节点数,判断其是否为公共祖先节点。

AC 代码

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
private:
// nodes:待求公共祖先节点的子节点
// nodesInTree: root 子树中包含的 nodes 中的节点的数目
TreeNode* lowestCommonAncestor(TreeNode* root, const vector<TreeNode*>& nodes, int&& nodesInTree)
{
int nodesInTreeRef;
return lowestCommonAncestor(root, nodes, nodesInTreeRef);
}
TreeNode* lowestCommonAncestor(TreeNode* root, const vector<TreeNode*>& nodes, int& nodesInTree)
{
if(root == nullptr)
{
return nullptr;
}

TreeNode* commonAncestor = nullptr;

// LCA 在 root 的左子树
int nodesInLeftTree = 0;
commonAncestor = lowestCommonAncestor(root->left, nodes, nodesInLeftTree);
if(nodesInLeftTree == nodes.size())
{
nodesInTree = nodesInLeftTree;
return commonAncestor;
}

// LCA 在 root 的右子树
int nodesInRightTree = 0;
commonAncestor = lowestCommonAncestor(root->right, nodes, nodesInRightTree);
if(nodesInRightTree == nodes.size())
{
nodesInTree = nodesInRightTree;
return commonAncestor;
}

// root 为 LCA
nodesInTree = nodesInLeftTree+nodesInRightTree;
if(find(nodes.begin(), nodes.end(), root) != nodes.end())
{
++nodesInTree;
}
if(nodesInTree == nodes.size()) return root;

return nullptr;
}
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
{
return lowestCommonAncestor(root, {p, q}, 0);
}
};


标签:Lowest,Binary,lowestCommonAncestor,TreeNode,nodesInTree,Tree,LCA,nodes,root
From: https://blog.51cto.com/u_15903085/5913216

相关文章

  • DevExpress TreeList使用心得
    最近做项目新增光纤线路清查功能模块,思路和算法已经想好了,些代码时候居然在一个控件上纠结了好长的时间,虽然后来搞定了,但是好记性不然烂笔头,还是写下来,以后要用到的时候直接......
  • DevExpress TreeList 调优_绑定数据源方式, 放弃原来的AppendNode加载数据的方式
    注意事项1:由于一旦绑定了数据源dataTable的些许变化便在TreeList中有所体现,所以等dataTable完全填充好了之后再绑定数据源.注意事项2:dataTable每行的父节点ID当加载到......
  • DevExpress 给TreeList添加右键菜单
    只有在右击节点时才会触发privatevoidtreeList1_MouseDown(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Right)......
  • 操作Devexpress treelist中的项
    如果需要在单元格添加时则用TreeList如果只是单纯读取数据或检索数据时则用GridControl​​​​1.如果点击添加时则添加TreeList的节点:protectedinternalvoidbtnAdd_Cli......
  • P4115 Qtree4
    \(\mathcalLink\)由于边权为负,因此使用DP求答案。需维护每个点的最大和次大位置,单次修改\(\mathcalO(n)\)。复杂度过劣。考虑到所求为路径,考虑链分治。设\(D(i)\)......
  • Vue-treeselect 实现下拉树懒加载,末节点不要箭头
    项目需要,可选择的下拉树,由于数据过多,显示要有层级感,所以使用了懒加载模式vue-treeselect官网:https://www.vue-treeselect.cn/1、前端代码1、下载依赖npminstall--sav......
  • pyprof2calltree和KCachegrind在windows上的使用
    win10windows似乎没有KCachegrind,有一个替代品叫做qcachegrind可以在这里下载https://sourceforge.net/projects/qcachegrindwin/解压,然后需要添加qcachegrind.exe的pat......
  • Treewidget 节点的遍历
    父节点的遍历  //Treewidget遍历操作  //只遍历父节点   intnParentNodeCount=ui->treeWidget->topLevelItemCount();   for(intnIndex=0;nInd......
  • Treewidget节点的增加
    父节点的创建   //隐藏QTreewidget标题头   ui->treeWidget->header()->hide();   //实现Treewidget父节点的挂载   //创建存放QTreewidget的容器......
  • Treewidget节点的删除
    父节点的删除    //第一种   //树状列表父节点的删除   //有点莽不支持这种操作deleteui->treeWidget->topLevelItem(0);    // 第二种 ......