码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode //C - 236. Lowest Common Ancestor of a Binary Tree


    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 , 1 0 5 2, 10^5 2,105].
    • − 1 0 9 < = N o d e . v a l < = 1 0 9 -10^9 <= Node.val <= 10^9 −109<=Node.val<=109
    • All Node.val are unique.
    • p != q
    • p and q will exist in the tree.

    From: LeetCode
    Link: 236. Lowest Common Ancestor of a Binary Tree


    Solution:

    Ideas:
    1. Start from the root of the tree.
    2. If the current node is either p or q, then return the current node. This means that we’ve found one of the two nodes.
    3. Recursively check the left and right child of the current node.
    4. If both left and right child recursive calls return non-NULL values, this means both p and q are found in different subtrees. Thus, the current node is their LCA.
    5. If only one of the left or right child recursive calls returns a non-NULL value, return that non-NULL value. This means one of the nodes is found in a subtree.
    Code:
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
        // Base case: if root is NULL or root is either p or q, return root
        if (!root || root == p || root == q) {
            return root;
        }
        
        // Recursively check left and right subtrees
        struct TreeNode* left = lowestCommonAncestor(root->left, p, q);
        struct TreeNode* right = lowestCommonAncestor(root->right, p, q);
        
        // If both left and right recursive calls return non-NULL, then root is the LCA
        if (left && right) {
            return root;
        }
        
        // Otherwise, return the non-NULL child
        return left ? left : right;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    Java中关于StringBuffer和StringBuilder的使用
    YOLOv9改进策略 | 添加注意力篇 | TripletAttention三重注意力机制(附代码+机制原理+添加教程)
    阈值与平滑处理
    服务器端请求伪造(SSRF)
    记录:2022-9-10 完美数 快乐数 括号生成 请求调页 页面置换 写时复制 页面缓冲算法
    android HAL层崩溃排查记录
    十年测试老鸟聊聊移动端兼容性测试
    ppt中的字体,如何批量替换?
    图像识别与处理学习笔记(五)人工神经网络和深度学习
    C++运算符重载详解(日期类实操)
  • 原文地址:https://blog.csdn.net/navicheung/article/details/132844355
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号