avatar
p*8
1
今天看career up 150题的时候做到平衡树的题目。
他是这样描述的
Implement a function to check if a tree is balanced For the purposes of this
question, a balanced tree is defined to be a tree such that no two leaf
nodes differ in distance from the root by more than one.
想请问大家下边这样的算是平衡吗?因为我记得以前的定义是说每个结点的左右子树深
度之差不超过1。下边的这棵树好像不符合这个定义但是符合150题上的描述吧。
1
2
3
4
这样的呢?谢谢大家了。
1
2 3
4 5 6 7
8 9 10 11
12
avatar
l*c
2
first one is not
second one is good
avatar
h*y
3
first one is not, second one is

this

【在 p**********8 的大作中提到】
: 今天看career up 150题的时候做到平衡树的题目。
: 他是这样描述的
: Implement a function to check if a tree is balanced For the purposes of this
: question, a balanced tree is defined to be a tree such that no two leaf
: nodes differ in distance from the root by more than one.
: 想请问大家下边这样的算是平衡吗?因为我记得以前的定义是说每个结点的左右子树深
: 度之差不超过1。下边的这棵树好像不符合这个定义但是符合150题上的描述吧。
: 1
: 2
: 3

avatar
s*y
4
嗯。似乎就是所有叶子最多只能高度差1吧。 但是对于只有1个叶子的这种情况(the
first one),可能height超过2就算不平衡了。 我是这么理解。
avatar
l*c
5
for your first example, you could think the root's right-child's height is 0
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。