当前位置: 首页 > news >正文

Leetcode--Lowest Common Ancestor of a Binary Search Tree

题目描述:


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


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




即在一个BST中,找到两个节点p和q的最小公共祖先。


思路:


由于是BST,因此可以分别求出p和q的查找路径pPath,qPath
找出pPath和qPath的第一个不同节点即可。




实现代码:




/**
 * 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) {
        var pathP = new List<TreeNode>();
        Path(root, p, pathP);
        var pathQ = new List<TreeNode>();
        Path(root, q , pathQ);
		
        var i = 0;
        while(i < pathP.Count || i < pathQ.Count){
			if(i >= pathP.Count ||i >= pathQ.Count){
				return pathP[--i];
			}
			
            if(pathP[i].val == pathQ[i].val){
                i ++;
            }
            else{
                return pathP[--i];
            }
        }
        return null;
    }
    
    public void Path(TreeNode node, TreeNode n, IList<TreeNode> list){
        if(node == null){
            return ;
        }
        list.Add(node);
		if(n.val == node.val){
			return;
		}
        if(n.val < node.val){
            Path(node.left, n ,list);
        }
        else{
            Path(node.right, n ,list);
        }
    }
    
}


相关文章:

  • 参加Tibco的SOA应用及2009 IT架构趋势研讨会记
  • Leet 题目整理归类 - 快速通道 (持续更新)
  • 设计模式的阴谋论
  • c# mongodb driver 插入重复记录
  • 中国移动通信信息资源站实体与互联网短消息网关(ISMG)
  • MongoDB C# Driver 使用示例 (2.2)
  • C# GetHashCode 的实现方式
  • SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY的区别比较
  • Dynamic Language Runtime (DLR) 初深
  • 2009 Webware 100 名单揭晓
  • DLR之 ExpandoObject和DynamicObject的使用示例
  • asp.net常用正则表达式
  • .Net 中Partitioner static与dynamic的性能对比
  • c# 中的 for vs foreach
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • [笔记] php常见简单功能及函数
  • Flannel解读
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • js算法-归并排序(merge_sort)
  • KMP算法及优化
  • LeetCode29.两数相除 JavaScript
  • nfs客户端进程变D,延伸linux的lock
  • SwizzleMethod 黑魔法
  • WinRAR存在严重的安全漏洞影响5亿用户
  • 工作手记之html2canvas使用概述
  • 学习Vue.js的五个小例子
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 因为阿里,他们成了“杭漂”
  • 原生Ajax
  • 最简单的无缝轮播
  • kubernetes资源对象--ingress
  • 昨天1024程序员节,我故意写了个死循环~
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • #include<初见C语言之指针(5)>
  • #Linux(权限管理)
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第2节(共同的基类)
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (pojstep1.1.2)2654(直叙式模拟)
  • (ZT)一个美国文科博士的YardLife
  • (机器学习-深度学习快速入门)第一章第一节:Python环境和数据分析
  • (三维重建学习)已有位姿放入colmap和3D Gaussian Splatting训练
  • (实战篇)如何缓存数据
  • (未解决)macOS matplotlib 中文是方框
  • (原)Matlab的svmtrain和svmclassify
  • (转载)(官方)UE4--图像编程----着色器开发
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .NET MVC 验证码
  • .net refrector
  • .NET 事件模型教程(二)
  • .NET多线程执行函数
  • /proc/stat文件详解(翻译)
  • ??eclipse的安装配置问题!??
  • @EnableWebMvc介绍和使用详细demo