- 2023-08-26剑指 Offer 68 - II. 二叉树的最近公共祖先(简单)
题目:classSolution{public:TreeNode*lowestCommonAncestor(TreeNode*root,TreeNode*p,TreeNode*q){if(root==p||root==q||root==nullptr)returnroot;//如果当前节点为空或者当前节点即为其中某个指定节点TreeNode*left=lowestCommo
- 2023-08-25二叉搜索树的公共祖先
力扣(LeetCode)官网-全球极客挚爱的技术成长平台1TreeNode*lowestCommonAncestor(TreeNode*root,TreeNode*p,TreeNode*q){2if(root==nullptr||root==p||root==q)returnroot;3//如果该结点比两个结点都大或则都小,则只需要搜索右子树或左子树,而
- 2023-08-20剑指 Offer 68 - I. 二叉搜索树的最近公共祖先(简单)
题目:classSolution{public:TreeNode*lowestCommonAncestor(TreeNode*root,TreeNode*p,TreeNode*q){//依旧要利用二叉搜索树的性质if(root->val>p->val&&root->val>q->val)returnlowestCommonAncestor(root->left,p,q);/
- 2023-06-02LeetCode235. 二叉搜索树的最近公共祖先
classSolution{public:TreeNode*lowestCommonAncestor(TreeNode*root,TreeNode*p,TreeNode*q){if(p->val<root->val&&q->val<root->val)returnlowestCommonAncestor(root->left,p,q);if(p->v
- 2022-12-05LeetCode: 236. Lowest Common Ancestor of a Binary Tree
LeetCode:236.LowestCommonAncestorofaBinaryTree题目描述Givenabinarytree,findthelowestcommonancestor(LCA)oftwogivennodesinthetree.Accordi
- 2022-10-20二叉搜索树的最近公共祖先
一.递归publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNodep,TreeNodeq){if(root.val<p.val&&root.val<q.val)return