由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请问二叉搜索树如何找到两个点的最近祖先?
相关主题
Facebook Phone ScreenLowest Common Ancestor of multiple nodes in a binary tree
Lowest Common Ancestor回馈本版,新鲜店面,新题新气象
求教Leetcode题目:Lowest Common Ancestor热腾腾的 LinkedIn 电面题攒RP
一个老题binary tree找 lowest common ancestor 的code (请教一道google面试题
问题在哪儿啊 kth Node of BST,大家帮忙Uber 面经
发现一个很恶心的基础问题Amazon 打印给定node距离最近的K个nodes
Find the node with given value in binary tree in in-orderrecovery BST 不考虑相同值的情况么?
Lowest common ancestor of two nodes of Binary TreeG家intern电面新鲜面经
相关话题的讨论汇总
话题: node话题: root话题: ischild话题: treenode话题: return
进入JobHunting版参与讨论
1 (共1页)
a********n
发帖数: 1287
1
谢谢先。
S*******0
发帖数: 198
2
careerCup
//find lowest common ancestor
//if p and q are on the same side of a node, go to the node's child
//if p and q are on the different side of a node, return the node
//complexity: every node will be reached. Some are multiple times
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){
if(isChild(root.left, p) && isChild(root.left, q))
return LowestCommonAncestor(root.left, p, q);
if(isChild(root.right, p) && isChild(root.right, q))
return LowestCommonAncestor(root.right, p, q);
return root;
}
public boolean isChild(TreeNode root, TreeNode node){
if(root == null) return false;
if(root == node) return true;
return isChild(root.left, node) || isChild(root.right, node);
}
j*****j
发帖数: 201
3
这是Binary Tree的最近祖先寻找法,
对BST, 直接从root开始,找到一个节点的数据位于[a,b]之间即为a,b的最近祖先

){

【在 S*******0 的大作中提到】
: careerCup
: //find lowest common ancestor
: //if p and q are on the same side of a node, go to the node's child
: //if p and q are on the different side of a node, return the node
: //complexity: every node will be reached. Some are multiple times
: public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){
: if(isChild(root.left, p) && isChild(root.left, q))
: return LowestCommonAncestor(root.left, p, q);
: if(isChild(root.right, p) && isChild(root.right, q))
: return LowestCommonAncestor(root.right, p, q);

a********n
发帖数: 1287
4
thanks, 那这个简单多了。

【在 j*****j 的大作中提到】
: 这是Binary Tree的最近祖先寻找法,
: 对BST, 直接从root开始,找到一个节点的数据位于[a,b]之间即为a,b的最近祖先
:
: ){

n**e
发帖数: 116
5
Here is my implementation in C++. Just my two cents.
// -------------------------------------------------------------------------
---
// Name: lowestCommonAncestor
// Description: Given two values representing two node's values in a
binary
// search tree, find the lowest common ancestor of the two nodes.
//
// Assumption: We assume those two values are different and both exist in
the
// tree if the tree is not empty.
//
// Node: This algorithm returns root node if one of the values
equals to
// root's value. It can be changed to return NULL instead in this
case.
// -------------------------------------------------------------------------
---
Node* LowestCommonAncestor(Node* root, int value1, int value2) {
using std::max;
using std::min;
using std::cout;
if (!root) return root;
Node* prev = NULL; // Hold the parent node
int small = min(value1, value2);
int large = max(value1, value2);
while(root){
int nodeValue = root->data;
if (large < nodeValue) { // Both values are less than the current node
value
prev = root;
root = root->left; // Go to the left subtree
} else if (small > nodeValue) { // Both values are greater
prev = root;
root = root->right; // Go to the right subtree
} else {

// If the node is one of the two given nodes, their lowest common
ancestor
// is its parent which is prev here
return (nodeValue == value1 || nodeValue == value2) ? prev
: root;
}
}
}
S******t
发帖数: 151
6
我的想法是先dfs一次,记住访问时间的那个pair,然后先判断a是否为b的祖先以及b是
否为a的祖先 (括号嵌套定理),如果不是,沿着a的父亲往根走,判断每个点是否为b的
祖先即可。
S******t
发帖数: 151
7
哦……是BST……看复杂了……那LSS说的方法是比较好的了
S******t
发帖数: 151
8
我给的那个方法是对二叉树找祖先的 -0-
1 (共1页)
进入JobHunting版参与讨论
相关主题
G家intern电面新鲜面经问题在哪儿啊 kth Node of BST,大家帮忙
请问如何求binary tree的lowest common ancestor发现一个很恶心的基础问题
讨论 Lowest common ancestor of BSTFind the node with given value in binary tree in in-order
二叉树如何判断一个节点是不是在另外两个节点的path上。。。。好像是个老题了。。求解?Lowest common ancestor of two nodes of Binary Tree
Facebook Phone ScreenLowest Common Ancestor of multiple nodes in a binary tree
Lowest Common Ancestor回馈本版,新鲜店面,新题新气象
求教Leetcode题目:Lowest Common Ancestor热腾腾的 LinkedIn 电面题攒RP
一个老题binary tree找 lowest common ancestor 的code (请教一道google面试题
相关话题的讨论汇总
话题: node话题: root话题: ischild话题: treenode话题: return