【在 m*******4 的大作中提到】 : Write a function that returns the size of the largest subtree that is : complete. : 请问怎么解啊
m*4
6 楼
This is an extension problem. No answer
【在 w*******s 的大作中提到】 : EPI的题书上不是有答案吗
b*a
7 楼
I think your idea is correct with a tiny bug; what your algorithm is returning the largest perfect binary tree instead of the largest complete binary tree. With your algorithm, this problem can be solved in O(n) time I think.
Assume we know the height of each node, which can be computed on the fly with O(n). Then it's just an extensive case study on the 3 possible status of a subroot: incomplete, complete (but not perfect), perfect. if (!tree.left is not complete) { tree is not complete } else { if (tree.left is not perfect) { tree is not complete } else { if (tree.right is not complete) { tree is not complete } else { if (tree.right is not perfect) { if (tree.left.height == tree.right.height) { tree is complete but not perfect } else { tree is not complete } } else { if (tree.left.height == tree.right.height) { tree is perfect } else { tree is not complete } } } } }
【在 m*******4 的大作中提到】 : Write a function that returns the size of the largest subtree that is : complete. : 请问怎么解啊
l*6
9 楼
status if (tree.left is not perfect) { The tree can be complete if tree.left is not perfect that is tree.left is complete tree.right is perfect tree.left.height - tree .right.height = 1
【在 b***e 的大作中提到】 : Assume we know the height of each node, which can be computed on the fly : with O(n). Then it's just an extensive case study on the 3 possible status : of a subroot: incomplete, complete (but not perfect), perfect. : if (!tree.left is not complete) { : tree is not complete : } else { : if (tree.left is not perfect) { : tree is not complete : } else { : if (tree.right is not complete) {
b*e
10 楼
That's right, made up. Thanks.
tree
【在 l******6 的大作中提到】 : : status : if (tree.left is not perfect) { : The tree can be complete if tree.left is not perfect : that is tree.left is complete tree.right is perfect tree.left.height - tree : .right.height = 1