代码编织梦想

原题链接: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).”

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.

Example 3:

Input: root = [1,2], p = 1, q = 2
Output: 1

Constraints:

  • The number of nodes in the tree is in the range [2, 105].
  • -109 <= Node.val <= 109
  • All Node.val are unique.
  • p != q
  • p and q will exist in the tree.

方法一:递归

思路:

C++代码:

/**
 * 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 {
public:

    /*
    函数的功能:
    给定两个节点 p 和 q
    1. 如果 p 和 q 都存在,则返回它们的公共祖先
    2. 如果只存在一个,则返回存在的一个
    3. 如果 p 和 q 都不存在,则返回NULL
    */
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        // 递归的出口
        if(root == NULL)
            return NULL;

        // 如果只存在一个,则返回存在的一个
        if(root == p || root == q) 
            return root;
            
        // 可认为已经实现了左右子树算出的结果
        TreeNode* left =  lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);
       
        // 如果有一个为空 那么答案只看另一个
        if(left == NULL)
            return right;
        if(right == NULL)
            return left;

        // p和q在两侧 此时的root就是结果
        if(left && right) 
            return root;
        
        // 必须有返回值
        return NULL; 
    }
};
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cwtnice/article/details/127158436

leetcode#236. 二叉树的最近公共祖先_ivyyin的博客-爱代码爱编程

class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {

leetcode 236. 二叉树的最近公共祖先(c++)_durablehumor的博客-爱代码爱编程_二叉树的最近公共祖先c++

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也

leetcode 236. 二叉树的最近公共祖先 java实现 个人算法之旅_茜茜的龙叔的博客-爱代码爱编程

解题思路:想要找出两个节点的最近祖先节点。树,首先考虑递归遍历。重点递归的返回条件,即return的条件。 1,从顶层开始遍历,找到P或q返回,考虑在什么时候返回。即当遍历到p或q节点(他们变成了root节点时)返回root。 2,当前的数被全部遍历完毕时,如果p 、q 分别在左子树或者右子树,说明其最近公共祖先节点为最开始的根节点。 3,如果p

leetcode 236. 二叉树的最近公共祖先(java)-爱代码爱编程

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

leetcode236. 二叉树的最近公共祖先_每天学一点!的博客-爱代码爱编程

题目描述 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”   示例 例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,

【leetcode】236.二叉树的最近公共祖先 (python)_薪升贷农名工的博客-爱代码爱编程

https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/ 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可

leetcode 236.二叉树的最近公共祖先 Java-爱代码爱编程

二叉树的最近公共祖先 题目链接描述示例初始代码模板代码 题目链接 https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/ 描述 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点

LeetCode236. 二叉树的最近公共祖先-爱代码爱编程

题目难度:中等 题目描述: 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” /** * Definition for a binary t

leetcode236.二叉树的最近公共祖先 Python-爱代码爱编程

题目: 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” 示例: 输入:root = [3,5,1,6,2,0,8,null,null,7,4

Leetcode 236. 二叉树的最近公共祖先-爱代码爱编程

解法1: 解法1借鉴于力扣官方解法,这里只是记录一下 二叉树的最近公共祖先一共有三种情况: 1. p或者q在左子树 2. p或者q在右子树 3. 根自己就是p或者q,则另一个p或q在左子树中或是右子树中 记左子树为, 则右子树为,上述情况可表达为: 根据上面表达式,可以确定公共祖先。 当左子树包含p或q时,左子树为真,当右子树包含p或q

LeetCode 236. 二叉树的最近公共祖先(java)-爱代码爱编程

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” 示例 1: 输入:root = [3,5,1,6,2,0,8,null,null,7,4], p

LeetCode—236. 二叉树的最近公共祖先-爱代码爱编程

236. 二叉树的最近公共祖先 题目描述: 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” 考察重点:DFS搜索两个节点,当找到目标节点时返回1,表示当

java leetcode 236. 二叉树的最近公共祖先(递归)_naion的博客-爱代码爱编程

题目描述 236. 二叉树的最近公共祖先 - 力扣(LeetCode)https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为

leetcode 226. invert binary tree 翻转二叉树(简单)_okokabcd的博客-爱代码爱编程

一、题目大意 给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。 示例 1: 输入:root = [4,2,7,1,3,6,9] 输出:[4,7,2,9,6,3,1] 示例 2:

牛客每日刷题__18shou的博客-爱代码爱编程

✅作者简介:我是18shou,一名即将秋招的java实习生 ✨个人主页:_18shou 🔥系列专栏:牛客刷题专栏 👉 在线刷题面经模拟面试    题目 题目主要信息: 给出一组区间,区间包括起始点,要求将重叠的区间合并重叠后的区间按照起点位置升序排列 思路 方法: 排序+贪心(推荐使用) 知识点:贪心思想 贪心思想属于动态规划思

leetcode236. 二叉树的最近公共祖先(java递归)_236 二叉搜索树最近公共祖先 java-爱代码爱编程

一:题目 二:上码 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNo

【leetcode】-爱代码爱编程

1. 题目 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” 2. 示例 输入:root = [3,5,1,6,2,0,8,null