LeetCode 二叉树,两个子节点的最近的公共父节点
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
- Recursive Approach
递归方法
- Iterative using parent pointers
使用父指针进行迭代
- 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 二叉树,两个子节点的最近的公共父节点的更多相关文章
- 二叉树中两节点的最近公共父节点(360的c++一面问题)
面试官的问题:写一个函数 TreeNode* Find(TreeNode* root, TreeNode* p, TreeNode* q) ,返回二叉树中p和q的最近公共父节点. 本人反应:当时有点 ...
- [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 ...
- 笔试算法题(24):找出出现次数超过一半的元素 & 二叉树最近公共父节点
出题:数组中有一个数字出现的次数超过了数组长度的一半,请找出这个数字: 分析: 解法1:首先对数组进行排序,时间复杂度为O(NlogN),由于有一个数字出现次数超过了数组的一半,所以如果二分数组的话, ...
- LCA最小公共父节点的解题思路
LCA最小公共父节点解法: 1.二叉搜索树: 中序遍历是升序,前序遍历即按序插入建树的序列. 二叉搜索树建树最好用前序+中序,如果用前序建树,最坏情况会退化为线性表,超时. 最近公共祖先甲级: A11 ...
- 编写一个方法,输入DOM节点,返回包含所有父节点的一个数组
编写一个方法,输入DOM节点,返回包含所有父节点的一个数组 function getParentsNodes(element) { var parents = []; var getParentsNo ...
- jQuery之防止冒泡事件,冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件。
冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件. 下面是html代码部分: <body> <div id="content"> 外层div元素 ...
- LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- (LeetCode)两个链表的第一个公共节点
LeetCode上面的题目例如以下: Write a program to find the node at which the intersection of two singly linked l ...
- [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 ...
随机推荐
- 知乎社区核心业务 Golang 化实践 - 知乎 https://zhuanlan.zhihu.com/p/48039838
知乎社区核心业务 Golang 化实践 - 知乎 https://zhuanlan.zhihu.com/p/48039838
- Flash 终将谢幕:微软将于年底停止对 Flash 的支持
近日,微软宣布将于今年 12 月终止对 Adobe Flash Player 的支持,届时,微软旗下所有浏览器都将无法使用 Flash,Adobe 也不会在今年 12 月后发布安全更新.早在 2017 ...
- numpy、pandas学习二
#numpy中arrary与pandas中series.DataFrame区别#arrary生成数组,无索引.列名:series有索引,且仅能创建一维数组:DataFrame有索引.列名import ...
- redis性能优化、内存分析及优化
redis性能优化.内存分析及优化 1.优化网络延时 2.警惕执行时间长的操作 3.优化数据结构.使用正确的算法 4.考虑操作系统和硬件是否影响性能 5.考虑持久化带来的开销 5.1 RDB 全量持久 ...
- Spark——DataFrames,RDD,DataSets、广播变量与累加器
Spark--DataFrames,RDD,DataSets 一.弹性数据集(RDD) 创建RDD 1.1RDD的宽依赖和窄依赖 二.DataFrames 三.DataSets 四.什么时候使用Dat ...
- Microsoft Exchange远程代码执行漏洞(CVE-2020-16875)
Microsoft Exchange远程代码执行漏洞(CVE-2020-16875) 漏洞信息: 由于对cmdlet参数的验证不正确,Microsoft Exchange服务器中存在一个远程执行代码漏 ...
- C++ 标准模板库(STL):map
目录 4. map 4.1 map的定义 4.2 map容器内元素的访问 4.3 map常用函数实例解析 4.4 map的常见用途 4. map map翻译为映射,也是常用的STL容器. 在定义数组时 ...
- docker 搭建 nginx负载均衡
本文描述如何在一台机器上搭建nginx负载均衡,我将会启动3个nginx的docker,分别是1台前置nginx负责分发,后面2台负责处理请求. 首先我切换到/usr/local/docker/文件夹 ...
- 调试lcd时候给linux单板移植tslib
作者:良知犹存 转载授权以及围观:欢迎添加微信公众号:Conscience_Remains 总述 tslib背景: 在采用触摸屏的移动终端中,触摸屏性能的调试是个重要问题之一,因为电磁噪声的缘故,触 ...
- Codeforces Round #673 (Div. 2) B. Two Arrays(数学)
题目链接:https://codeforces.com/contest/1417/problem/B 题意 定义 $f(a)$ 为数组 $a$ 中满足: $i < j$ $a_i + a_j = ...