Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getsibling 方法的实现 #113

Closed
wuhao0120 opened this issue Jan 6, 2021 · 2 comments
Closed

getsibling 方法的实现 #113

wuhao0120 opened this issue Jan 6, 2021 · 2 comments

Comments

@wuhao0120
Copy link

No description provided.

@wuhao0120 wuhao0120 reopened this Jan 6, 2021
@wuhao0120
Copy link
Author

`def get_siblings(self, node):

    assert isinstance(node,TreeNode)
    if node.parent:
        siblings = node.parent.children.remove(node)
        return siblings
    else:
        return []`

试运行报错 list.remove(x): x not in list
可是node为什么不在它的parent的children list里面呀

@neolee neolee changed the title getsibling方法的实现 getsibling 方法的实现 Jan 6, 2021
@neolee
Copy link
Owner

neolee commented Jan 6, 2021

这是大作业,所以我不能直接给你答案,但可以给你一些提示。

lst.remove(x) 这个方法会从列表 lst 中移除找到的第一个 x 元素,注意它直接改变列表对象 lst 的内容,但返回值是 None。所以你的 siblings = node.parent.children.remove(node) 这一句即危险(直接改变了 children 属性的内容使得树结构乱掉)又达不到你想要的效果(siblings 的值将为 None)。

@neolee neolee closed this as completed Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants