LeetCode实战:二叉树的最近公共祖先

news/2024/7/5 2:45:06

背景

  • 为什么你要加入一个技术团队?
  • 如何加入 LSGO 软件技术团队?
  • 我是如何组织“算法刻意练习活动”的?
  • 为什么要求团队的学生们写技术Blog

题目英文

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: root = [3,5,1,6,2,0,8,null,null,7,4]

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.

题目中文

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,null,7,4]

示例 1:

输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出: 3
解释: 节点 5 和节点 1 的最近公共祖先是节点 3

示例 2:

输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
输出: 5
解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。

说明:

  • 所有节点的值都是唯一的。
  • p、q 为不同节点且均存在于给定的二叉树中。

算法实现

/*** Definition for a binary tree node.* public class TreeNode {*     public int val;*     public TreeNode left;*     public TreeNode right;*     public TreeNode(int x) { val = x; }* }*/public class Solution
{public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){return Find(root, p, q);}private TreeNode Find(TreeNode current, TreeNode p, TreeNode q){if (current == null || current == p || current == q)return current;TreeNode left = Find(current.left, p, q);TreeNode right = Find(current.right, p, q);if (left != null && right != null)return current;return left != null ? left : right;}
}

实验结果

  • 状态:通过
  • 31 / 31 个通过测试用例
  • 执行用时: 132 ms, 在所有 C# 提交中击败了 96.10% 的用户
  • 内存消耗: 27.5 MB, 在所有 C# 提交中击败了 20.00% 的用户

提交结果


相关图文

1. “数组”类算法

  • LeetCode实战:三数之和
  • LeetCode实战:最接近的三数之和
  • LeetCode实战:求众数
  • LeetCode实战:缺失的第一个正数
  • LeetCode实战:快乐数
  • LeetCode实战:寻找两个有序数组的中位数
  • LeetCode实战:盛最多水的容器
  • LeetCode实战:删除排序数组中的重复项
  • LeetCode实战:搜索旋转排序数组
  • LeetCode实战:螺旋矩阵
  • LeetCode实战:螺旋矩阵 II
  • LeetCode实战:买卖股票的最佳时机
  • LeetCode实战:买卖股票的最佳时机 II

2. “链表”类算法

  • LeetCode实战:两数相加
  • LeetCode实战:删除链表的倒数第N个节点
  • LeetCode实战:两两交换链表中的节点
  • LeetCode实战:旋转链表
  • LeetCode实战:环形链表

3. “栈”类算法

  • LeetCode实战:有效的括号
  • LeetCode实战:最长有效括号
  • LeetCode实战:逆波兰表达式求值

4. “队列”类算法

  • LeetCode实战:设计循环双端队列
  • LeetCode实战:滑动窗口最大值
  • LeetCode实战:整数反转
  • LeetCode实战:字符串转换整数 (atoi)

5. “递归”类算法

  • LeetCode实战:爬楼梯

6. “位运算”类算法

  • LeetCode实战:只出现一次的数字
  • LeetCode实战:格雷编码

7. “字符串”类算法

  • LeetCode实战:反转字符串
  • LeetCode实战:翻转字符串里的单词
  • LeetCode实战:最长公共前缀
  • LeetCode实战:字符串相加
  • LeetCode实战:字符串相乘

8. “树”类算法

  • LeetCode实战:相同的树
  • LeetCode实战:对称二叉树
  • LeetCode实战:二叉树的最大深度
  • LeetCode实战:二叉树中的最大路径和
  • LeetCode实战:将有序数组转换为二叉搜索树

9. “哈希”类算法

  • LeetCode实战:两数之和

10. “排序”类算法

  • LeetCode实战:合并两个有序数组
  • LeetCode实战:合并两个有序链表
  • LeetCode实战:合并K个排序链表

11. “搜索”类算法

  • LeetCode实战:搜索二维矩阵
  • LeetCode实战:子集

12. “动态规划”类算法

  • LeetCode实战:最长回文子串
  • LeetCode实战:最大子序和
  • LeetCode实战:不同路径

13. “回溯”类算法

  • LeetCode实战:全排列

14. “数值分析”类算法

  • LeetCode实战:回文数
  • LeetCode实战:x 的平方根

http://lihuaxi.xjx100.cn/news/255074.html

相关文章

SpringBoot+RabbitMQ 死信队列

欢迎关注方志朋的博客,回复”666“获面试宝典前言死信:无法被消费的消息,称为死信。如果死信一直留在队列中,会导致一直被消费,却从不消费成功。所以我们专门开辟了一个来存放死信的队列,叫死信队列&#x…

来厦门了!线上交流限额免费报名中

Datawhale活动 参与:线上或厦门线下,时间:6月23日6月23日,飞桨中国行将走进厦门,直击企业智能化转型升级诉求,围绕工业质检、安全巡检、生产设备健康管理等问题,由政府专家、行业大咖等深入剖析…

户外广告新创意

近来,各大城市纷纷加大了对户外广告的监管力度,部分城市甚至停止审批户外广告牌。这让户外广告运营者和广告发布商甚为头疼。 长期以来,户外广告牌扮演着截然相反的“双重角色”,在户外广告运营者和广告发布商眼中,“寸…

ES选主策略

ES版本5.6.3 1、整个流程的开始,实在node启动后触发的,Node.java中start()方法,通过调用ZenDiscovery.java中的doStart()方法,之后会调用startInitialJoin方法开始进行加入现有的cluster或者选主。 public void startInitialJoin(…

驱动数字经济加速,摩尔线程发布全新元计算架构MUSA和GPU产品

2022年3月30日,北京——摩尔线程今天举行主题为“元动力 创无限”的春季发布会。摩尔线程创始人兼CEO张建中解读了“元计算”这一产业趋势,并发布全新架构及系列重磅新品,包括:MUSA(Moore Threads Unified System Arch…

数字家庭开发者中心

数字家庭开发者中心 http://www.adobe.com/devnet/devices/digital_home.html转载于:https://www.cnblogs.com/kobo/archive/2010/07/06/1772136.html

MySQL5.7配置日志

之前使用MySQL 5.1版本的时候,修改my.cnf,在[mysqld]下添加"log/data/mysql/query.log",重启服务就ok了 但是在5.7会出现 Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).原因是5.7…

LeetCode实战:除自身以外数组的乘积

背景 为什么你要加入一个技术团队?如何加入 LSGO 软件技术团队?我是如何组织“算法刻意练习活动”的?为什么要求团队的学生们写技术Blog 题目英文 Given an array nums of n integers where n > 1, return an array output such that ou…