由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 二叉树如何判断一个节点是不是在另外两个节点的path上。。。。好像是个老题了。。求解?
相关主题
一道二叉树的老题binary tree, sum of 2 nodes == given number
求教一道老题求教Leetcode题目:Lowest Common Ancestor
问个老题,find the next larger in BSTLowest common ancestor of two nodes of Binary Tree
一个老题binary tree找 lowest common ancestor 的code (请教Lowest Common Ancestor of multiple nodes in a binary tree
MS面试题bloomberg onsite题
问个老题How to find the kth biggest number in a BST
F家电面在版上看到的G题
树 inorder下个节点最好办法是啥BST 找重复节点数
相关话题的讨论汇总
话题: path话题: nodes话题: get话题: traversal话题: 节点
进入JobHunting版参与讨论
1 (共1页)
e*****e
发帖数: 1275
1
There is a binary tree(Not a BST) in which you are given three nodes x,y,z .
Write a function which finds whether y lies in the path b/w x
m*********1
发帖数: 112
2
inorder traversal?
j*****u
发帖数: 1133
3
easy if Node has a parent pointer
if not, do a recursive traversal and maintain a set that contains the nodes
from root to current, and check x,y,z in the set during traversal. If we eve
r see nodes get added into set following x->z->y or or y->z->x order, we kno
w z is on the path between x and y. Time: O(N), Space: O(logN)

.

【在 e*****e 的大作中提到】
: There is a binary tree(Not a BST) in which you are given three nodes x,y,z .
: Write a function which finds whether y lies in the path b/w x

w*z
发帖数: 75
4
how about z is the parent of x and y, and x and y are in different subtrees

站: BBS 未名空间站 (Fri Feb 4 00:49:26 2011, 美东)
nodes
eve
kno
,z

【在 j*****u 的大作中提到】
: easy if Node has a parent pointer
: if not, do a recursive traversal and maintain a set that contains the nodes
: from root to current, and check x,y,z in the set during traversal. If we eve
: r see nodes get added into set following x->z->y or or y->z->x order, we kno
: w z is on the path between x and y. Time: O(N), Space: O(logN)
:
: .

j*****u
发帖数: 1133
5
Then I misunderstood the question, in this case
1. Get path from root -> x and root -> y
2. Get the lowest common ancestor w of x and y
3. Check if z is on x -> w or w -> y
Complexity is still the same

subtrees

【在 w*z 的大作中提到】
: how about z is the parent of x and y, and x and y are in different subtrees
:
: 站: BBS 未名空间站 (Fri Feb 4 00:49:26 2011, 美东)
: nodes
: eve
: kno
: ,z

1 (共1页)
进入JobHunting版参与讨论
相关主题
BST 找重复节点数MS面试题
用bst怎么实现hashtable?问个老题
一个GOOG的二叉树面试题F家电面
google电面(挂了)树 inorder下个节点最好办法是啥
一道二叉树的老题binary tree, sum of 2 nodes == given number
求教一道老题求教Leetcode题目:Lowest Common Ancestor
问个老题,find the next larger in BSTLowest common ancestor of two nodes of Binary Tree
一个老题binary tree找 lowest common ancestor 的code (请教Lowest Common Ancestor of multiple nodes in a binary tree
相关话题的讨论汇总
话题: path话题: nodes话题: get话题: traversal话题: 节点