Redian新闻
>
shopper 11月cashback还没寄出来?
avatar
shopper 11月cashback还没寄出来?# Money - 海外理财
S*e
1
glassdoor上看到的,这题如果有parent point还比较简单,
如果没有的话怎么整?
avatar
w*u
2
都快月底了。
avatar
h*n
3
iterative in order traversal.

【在 S*******e 的大作中提到】
: glassdoor上看到的,这题如果有parent point还比较简单,
: 如果没有的话怎么整?

avatar
s*t
4
我的都存掉了

【在 w***u 的大作中提到】
: 都快月底了。
avatar
w*x
5
stack
avatar
m*y
6
19号就算月底了?发这样的贴有啥意义?等等不就来了?
avatar
S*e
7
多谢两位,大概写了一下,能帮们确认一下吗?
Node *findInOrderSuccessor(Node *root, Node *target){
Stack s;
Node *cur = root;
Node *prev = NULL;
while(true){
while(cur) {
s.push(cur);
cur = cur->left;
}
if (s.empty()){
break;
} else {
cur = s.pop();
}
if (prev == target) {
return cur;
}
prev = cur;
cur = cur->right;
}
return NULL;
}

【在 w****x 的大作中提到】
: stack
avatar
y*g
8
Isn't the answer leftmost node of target node's right child tree??

【在 S*******e 的大作中提到】
: glassdoor上看到的,这题如果有parent point还比较简单,
: 如果没有的话怎么整?

avatar
y*1
9
I think that's the answer, but you have to handle the case when the node
does not have right node. In that case, use breadth-first traversal.

【在 y****g 的大作中提到】
: Isn't the answer leftmost node of target node's right child tree??
avatar
t*3
10
if this node has right subtree, get the min node from that.
if not, travel from root node to current node, store the path in some kind
of list/or stack etc. iterate back according to the list/stack, the
successor is the first right parent node
avatar
p*2
11
一会儿做做。
avatar
p*2
12
想了一下。这题应该用recursion吧?
avatar
p*2
13
写了一个练练手
def first(root):
if root!=None:
l=first(root.left)
if l: return l
return root
def dfs(root,node):
if root==None: return root

if root==node:
r=first(root.right)
return r if r else root
else:
l=dfs(root.left,node)
if l: return l if l!=node else root
else: return dfs(root.right,node)

def next(root,node):
ret=dfs(root,node)
return ret if ret!=node else None
avatar
i*e
14
你好像忘了考虑当 node 是 subtree 中的最右边值 (也就是它的 next inorder 是返
回 subtree->parent)。

【在 p*****2 的大作中提到】
: 写了一个练练手
: def first(root):
: if root!=None:
: l=first(root.left)
: if l: return l
: return root
: def dfs(root,node):
: if root==None: return root
:
: if root==node:

avatar
p*2
15

没明白什么意思呀。给个例子?

【在 i**********e 的大作中提到】
: 你好像忘了考虑当 node 是 subtree 中的最右边值 (也就是它的 next inorder 是返
: 回 subtree->parent)。

avatar
p*2
16

当node 是subtree的最右边值,我会返回node的。告诉上边我找到node了,但是没找到
next。
如果这个subtree恰好是上一层的左数,那就返回上一层的node,不然继续返回node往
上走。

【在 i**********e 的大作中提到】
: 你好像忘了考虑当 node 是 subtree 中的最右边值 (也就是它的 next inorder 是返
: 回 subtree->parent)。

avatar
i*e
17
呵呵,少看了一个条件。没事,你是考虑了这个条件的。
另外,这个dfs 不好写吧,很容易出错,你代码太多隐含条件。还有一个担心的是效率
问题。例如,一个 node 如果有 left node,直接返回 left-most node 即可,但你的
程序
每次要从root 开始.
只有在 node 是 leaf node 的情况之下 才需要从 root 开始。这个可以递归或者用
stack,没必要搞得那么复杂。
avatar
p*2
18

例如,一个 node 如果有 left node,直接返回 left-most node 即可
>>这个不是inorder吗?怎么会直接返回left-most?
只有在 node 是 leaf node 的情况之下 才需要从 root 开始
>>如果node没有right node的话也需要从root开始呀。
感觉只在node有right的情况呀,才能能直接返回吧?这种情况应该可以wrap一下就包
括了吧。一会儿看看。

【在 i**********e 的大作中提到】
: 呵呵,少看了一个条件。没事,你是考虑了这个条件的。
: 另外,这个dfs 不好写吧,很容易出错,你代码太多隐含条件。还有一个担心的是效率
: 问题。例如,一个 node 如果有 left node,直接返回 left-most node 即可,但你的
: 程序
: 每次要从root 开始.
: 只有在 node 是 leaf node 的情况之下 才需要从 root 开始。这个可以递归或者用
: stack,没必要搞得那么复杂。

avatar
i*e
19
不好意思,你是对的。
是next inorder,所以不看left subtree。
主要看有没有right child,有的话返回right subtree的 left most node。不然的话
得找出包含这个 node 为 right-most node 的subtree 的 parent,得用递归或者
stack(非递归) 来做。
avatar
Z*Z
20
响应大侠号召。写了个直白板的,求拍。
static >Node next(Node tree, Node node) {
if(tree == null){
return null;
}
if(node.right != null){
Node tmp = node.right;
while(tmp.left != null){
tmp = tmp.left;
}
return tmp;
}
Stack> stk = new Stack>();
if(!searchNode(tree, node, stk)){
return null;
}
Node child = stk.pop();
while(!stk.isEmpty()){
Node parent = stk.pop();
if(child == parent.left){
return parent;
}
child = parent;
}
return null;
}
static > boolean searchNode(Node tree, Node no
de, Stack> stk){
if(tree == null){
return false;
}
stk.push(tree);
if(tree == node ||
searchNode(tree.left, node, stk) ||
searchNode(tree.right, node, stk)){
return true;
}
stk.pop();
return false;
}

【在 i**********e 的大作中提到】
: 呵呵,少看了一个条件。没事,你是考虑了这个条件的。
: 另外,这个dfs 不好写吧,很容易出错,你代码太多隐含条件。还有一个担心的是效率
: 问题。例如,一个 node 如果有 left node,直接返回 left-most node 即可,但你的
: 程序
: 每次要从root 开始.
: 只有在 node 是 leaf node 的情况之下 才需要从 root 开始。这个可以递归或者用
: stack,没必要搞得那么复杂。

avatar
S*e
21
这样recrusion好像是对的,不过像我前面那样直接用
stack iterative in-order traversal好像更简单啊。
我的做法有问题吗?
http://www.mitbbs.com/article/JobHunting/32160147_0.html

【在 p*****2 的大作中提到】
: 写了一个练练手
: def first(root):
: if root!=None:
: l=first(root.left)
: if l: return l
: return root
: def dfs(root,node):
: if root==None: return root
:
: if root==node:

avatar
j*e
22
好像没问题,至少我没看出来

【在 S*******e 的大作中提到】
: 这样recrusion好像是对的,不过像我前面那样直接用
: stack iterative in-order traversal好像更简单啊。
: 我的做法有问题吗?
: http://www.mitbbs.com/article/JobHunting/32160147_0.html

avatar
p*2
23

stack是正确的解法。leetcode已经总结了。不过我看到这题的第一感觉就是dfs,所以
就写出来了。这个跟个人的思维习惯有关系,我做dfs比较熟。

【在 S*******e 的大作中提到】
: 这样recrusion好像是对的,不过像我前面那样直接用
: stack iterative in-order traversal好像更简单啊。
: 我的做法有问题吗?
: http://www.mitbbs.com/article/JobHunting/32160147_0.html

avatar
p*2
24

嗯。leetcode总结的很好。这个优化应该有。

【在 i**********e 的大作中提到】
: 不好意思,你是对的。
: 是next inorder,所以不看left subtree。
: 主要看有没有right child,有的话返回right subtree的 left most node。不然的话
: 得找出包含这个 node 为 right-most node 的subtree 的 parent,得用递归或者
: stack(非递归) 来做。

avatar
S*e
25
哦,多谢

【在 p*****2 的大作中提到】
:
: 嗯。leetcode总结的很好。这个优化应该有。

avatar
i*e
26
小心别把面试官给震经了。
2爷:
“这么简单的题我一向来都是直接上dfs的!”
面试官直接跪了。。。

【在 p*****2 的大作中提到】
:
: 嗯。leetcode总结的很好。这个优化应该有。

avatar
w*x
27
5 minutes
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
class BTIterator
{
public:
void Init(NODE* pRoot)
{ PushLefts(pRoot); }
NODE* Next()
{
if (m_stk.empty())
return NULL;
NODE* pRet = m_stk.top();
m_stk.pop();
PushLefts(pRet->pRgt);
return pRet;
}
private:
void PushLefts(NODE* pNode)
{
while (NULL != pNode)
{
m_stk.push(pNode);
pNode = pNode->pLft;
}
}
private:
stack m_stk;
};
avatar
p*2
28

刚开始我以为写个class,像你这样。后来觉得写给定一个node找下一个更难一些。

【在 w****x 的大作中提到】
: 5 minutes
: struct NODE
: {
: int nVal;
: NODE* pLft;
: NODE* pRgt;
: NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
: };
: class BTIterator
: {

avatar
p*2
29

刚开始我以为写个class,像你这样。后来觉得写给定一个node找下一个更难一些。

【在 w****x 的大作中提到】
: 5 minutes
: struct NODE
: {
: int nVal;
: NODE* pLft;
: NODE* pRgt;
: NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
: };
: class BTIterator
: {

avatar
w*x
30

滔滔江水连绵不绝,黄河泛滥一发不可收拾啊~~

【在 p*****2 的大作中提到】
:
: 刚开始我以为写个class,像你这样。后来觉得写给定一个node找下一个更难一些。

avatar
p*2
31

看你整天说没题可做了。感觉很膜拜你。

【在 w****x 的大作中提到】
:
: 滔滔江水连绵不绝,黄河泛滥一发不可收拾啊~~

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。