由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - BST 找重复节点数
相关主题
树 inorder下个节点最好办法是啥问两道facebook面试题
How to find the kth biggest number in a BST为何找不到很多apple的面筋
recovery BST 不考虑相同值的情况么?关于inordertraversal 的iterative way
G家onsite面经Find number of subtrees with the same value
recover binary search tree 常数空间FB面试题:binary tree inorder successor
请教一个BST找Median的题目问一道二叉树遍历的问题? 谢谢!
一个很简单的问题,有没有快一点的算法?这个Binary Tree的题来看看
Google Front-end Software Engineer Phone Interview请问如何求binary tree的lowest common ancestor
相关话题的讨论汇总
话题: bst话题: node话题: inorderdfs话题: mutableint话题: binary
进入JobHunting版参与讨论
1 (共1页)
b*****u
发帖数: 648
1
要求 O(n)时间 O(1)空间,怎么搞?
glassdoor上看到的
http://www.glassdoor.com/Interview/count-the-number-of-duplicat
t*********n
发帖数: 89
2
中序遍历,记录前一个节点,比较后记数?
p*****2
发帖数: 21240
3
traverse一遍就可以了吧?
b*****u
发帖数: 648
4
那样达不到O(1)吧

【在 p*****2 的大作中提到】
: traverse一遍就可以了吧?
p*****2
发帖数: 21240
5

两个变量就可以了吧?

【在 b*****u 的大作中提到】
: 那样达不到O(1)吧
e****e
发帖数: 418
6
Assume there is only one value got duplicated.
global variable: int count = 0;
void inorderDFS(Node n, MutableInt prev) {
if ( node == null )
return;
inorderDFS( node.left, prev );
if ( n.val == prev.val )
count++;
pre.val = node.val;
inorderDFS( node.right, prev );
}
invoke: inorderDFS( root, new MutableInt() );
return: count
class MutableInt {
int val;
}
l*****a
发帖数: 14598
7
讨论一下
BST到底是否允许有重复元素

【在 b*****u 的大作中提到】
: 要求 O(n)时间 O(1)空间,怎么搞?
: glassdoor上看到的
: http://www.glassdoor.com/Interview/count-the-number-of-duplicat

G****A
发帖数: 4160
8
原题目是 "count the number of duplicates in a binary tree in O(n) time O(1)
space"
没看到说BST啊

【在 b*****u 的大作中提到】
: 要求 O(n)时间 O(1)空间,怎么搞?
: glassdoor上看到的
: http://www.glassdoor.com/Interview/count-the-number-of-duplicat

e****e
发帖数: 418
9
by Interview Candidate: Oct 24, 2012
yes you are correct, I meant a BST

)

【在 G****A 的大作中提到】
: 原题目是 "count the number of duplicates in a binary tree in O(n) time O(1)
: space"
: 没看到说BST啊

l*****a
发帖数: 14598
10
In computer science, a binary search tree (BST), which may sometimes also be
called an ordered or sorted binary tree, is a node-based binary tree data
structure which has the following properties:[1]
The left subtree of a node contains only nodes with keys less than the node'
s key.
The right subtree of a node contains only nodes with keys greater than the
node's key.
Both the left and right subtrees must also be binary search trees.
There must be no duplicate nodes.

【在 e****e 的大作中提到】
: by Interview Candidate: Oct 24, 2012
: yes you are correct, I meant a BST
:
: )

相关主题
请教一个BST找Median的题目问两道facebook面试题
一个很简单的问题,有没有快一点的算法?为何找不到很多apple的面筋
Google Front-end Software Engineer Phone Interview关于inordertraversal 的iterative way
进入JobHunting版参与讨论
e****e
发帖数: 418
11
在面试中, BST和wiki上的定义弱相关,和面试官头脑里的定义强相关。。。我们现在
也不知道当时的情况,就姑且认为是有一个节点可以重复的树,其他节点满足BST?

be
node'

【在 l*****a 的大作中提到】
: In computer science, a binary search tree (BST), which may sometimes also be
: called an ordered or sorted binary tree, is a node-based binary tree data
: structure which has the following properties:[1]
: The left subtree of a node contains only nodes with keys less than the node'
: s key.
: The right subtree of a node contains only nodes with keys greater than the
: node's key.
: Both the left and right subtrees must also be binary search trees.
: There must be no duplicate nodes.

G****A
发帖数: 4160
12
Now, I see.

【在 e****e 的大作中提到】
: by Interview Candidate: Oct 24, 2012
: yes you are correct, I meant a BST
:
: )

p*****2
发帖数: 21240
13

可以有重复

【在 l*****a 的大作中提到】
: 讨论一下
: BST到底是否允许有重复元素

l*****a
发帖数: 14598
14
哪本书?

【在 p*****2 的大作中提到】
:
: 可以有重复

p*****2
发帖数: 21240
15

CC150

【在 l*****a 的大作中提到】
: 哪本书?
p*****p
发帖数: 379
16
假设一样的走左树,两个变量怎么找下面这个?
(4, 2, #, 1, 3, #, 2, #, 4)

【在 p*****2 的大作中提到】
:
: CC150

p*****2
发帖数: 21240
17

能不能画个树出来看?

【在 p*****p 的大作中提到】
: 假设一样的走左树,两个变量怎么找下面这个?
: (4, 2, #, 1, 3, #, 2, #, 4)

p*****p
发帖数: 379
18


【在 p*****2 的大作中提到】
:
: 能不能画个树出来看?

p*****2
发帖数: 21240
19
1 2 2 3 4 4
这个顺序走吧?
e****e
发帖数: 418
20
我觉得是。

【在 p*****2 的大作中提到】
: 1 2 2 3 4 4
: 这个顺序走吧?

b*****u
发帖数: 648
21
两个变量的方法能不能elaborate一下或者贴个链接?
我目前搜到的不用递归,不用栈的方法都assume有父指针
p*****p
发帖数: 379
22
了解了,Inorder+两个变量就够了

【在 p*****2 的大作中提到】
: 1 2 2 3 4 4
: 这个顺序走吧?

1 (共1页)
进入JobHunting版参与讨论
相关主题
请问如何求binary tree的lowest common ancestorrecover binary search tree 常数空间
问个老题,find the next larger in BST请教一个BST找Median的题目
微软面试的一道题一个很简单的问题,有没有快一点的算法?
插入节点到complete binary tree的末尾Google Front-end Software Engineer Phone Interview
树 inorder下个节点最好办法是啥问两道facebook面试题
How to find the kth biggest number in a BST为何找不到很多apple的面筋
recovery BST 不考虑相同值的情况么?关于inordertraversal 的iterative way
G家onsite面经Find number of subtrees with the same value
相关话题的讨论汇总
话题: bst话题: node话题: inorderdfs话题: mutableint话题: binary