LeetCode 二叉树,两个子节点的最近的公共父节点

二叉树

Lowest Common Ancestor of a Binary Tree

二叉树的最近公共父亲节点

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree

https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
let ancestor;
// DFS 深度优先搜索
const dfs = (root, p, q) => {
if (root === null) {
return false;
}
// 递归
const leftSon = dfs(root.left, p, q);
const rightSon = dfs(root.right, p, q);
if ((leftSon && rightSon) || ((root.val === p.val || root.val === q.val) && (leftSon || rightSon))) {
ancestor = root;
}
return leftSon || rightSon || (root.val === p.val || root.val === q.val);
}
dfs(root, p, q);
return ancestor;
};


solutions

  1. Recursive Approach

递归方法



  1. Iterative using parent pointers

使用父指针进行迭代



  1. Iterative without parent pointers

没有父指针的迭代



refs

https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


LeetCode 二叉树,两个子节点的最近的公共父节点的更多相关文章

  1. 二叉树中两节点的最近公共父节点(360的c++一面问题)

    面试官的问题:写一个函数  TreeNode* Find(TreeNode* root, TreeNode* p, TreeNode* q) ,返回二叉树中p和q的最近公共父节点. 本人反应:当时有点 ...

  2. [LeetCode] 1123. Lowest Common Ancestor of Deepest Leaves 最深叶结点的最小公共父节点

    Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall that: Th ...

  3. 笔试算法题(24):找出出现次数超过一半的元素 & 二叉树最近公共父节点

    出题:数组中有一个数字出现的次数超过了数组长度的一半,请找出这个数字: 分析: 解法1:首先对数组进行排序,时间复杂度为O(NlogN),由于有一个数字出现次数超过了数组的一半,所以如果二分数组的话, ...

  4. LCA最小公共父节点的解题思路

    LCA最小公共父节点解法: 1.二叉搜索树: 中序遍历是升序,前序遍历即按序插入建树的序列. 二叉搜索树建树最好用前序+中序,如果用前序建树,最坏情况会退化为线性表,超时. 最近公共祖先甲级: A11 ...

  5. 编写一个方法,输入DOM节点,返回包含所有父节点的一个数组

    编写一个方法,输入DOM节点,返回包含所有父节点的一个数组 function getParentsNodes(element) { var parents = []; var getParentsNo ...

  6. jQuery之防止冒泡事件,冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件。

    冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件. 下面是html代码部分: <body> <div id="content"> 外层div元素 ...

  7. LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  8. (LeetCode)两个链表的第一个公共节点

    LeetCode上面的题目例如以下: Write a program to find the node at which the intersection of two singly linked l ...

  9. [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 ...

随机推荐

  1. Linux性能监测(系统监测统计命令详解)

    通过这个命令,可以最简便的看出系统当前基本状态信息,这里面最有用是负载指标,如果你还想查看当前系统的CPU/内存以及相关的进程状态,可以使用TOP命令. TOP 通过TOP命令可以详细看出当前系统的C ...

  2. 简话http请求

    一.http请求概念: 1.是指从客户端到服务器端的请求消息.包括:消息首行中,对资源的请求方法.资源的标识符及使用的协议. 包括请求(request)和响应(response) 2.过程: 域名解析 ...

  3. Building a high performance JSON parser

    Building a high performance JSON parser https://dave.cheney.net/high-performance-json.html

  4. redis 代码结构与阅读顺序

    https://www.cnblogs.com/aixiaomei/p/6311633.html

  5. 第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛(同步赛)

    传送门 B-小宝的幸运数组 题目描述 对于小宝来说,如果一个数组的总和能够整除他的幸运数字k,就是他的幸运数组,而其他数组小宝都很讨厌.现在有一个长度为n的数组,小宝想知道这个数组的子数组中,最长的幸 ...

  6. (十八)整合Nacos组件,环境搭建和入门案例详解

    整合Nacos组件,环境搭建和入门案例详解 1.Nacos基础简介 1.1 关键特性 1.2 专业术语解释 1.3 Nacos生态圈 2.SpringBoot整合Nacos 2.1 新建配置 2.2 ...

  7. Django(多表查询操作)

    首先了解一下 mysql中的表之间的关系,一对一,一对多,多对一,多对多. 一对多关系.多对一关系.一对一关系 至少都有一侧是单个实体,所以记录之间的联系通过外键实现,让外键指向这个实体. 实现这种关 ...

  8. Cisco交换机管理员配置

    conf tservice timestamps debug datetime msec localtime yearservice timestamps log datetime msec loca ...

  9. 搭建 mariadb 数据库主从同步

    一.主(master)数据库配置 1. my.cnf 添加配置 [mariadb] log-bin server_id=1 log-basename=master1 binlog-format=mix ...

  10. JVM探究——转载

    JVM探究 请你谈谈你对JVM的理解 Java8虚拟机和之前的变化更新 什么式OOM,什么是栈溢出StackOverFlowError?怎么分析 JVM的常用调优参数有哪些? 内存快照如何抓取,怎么分 ...