我已经编写了AVL树代码,但是如何编写代码来确定我的树是否不平衡,并且找到不平衡类型,即“左偏左”,“左偏右”,“右偏右”和“右偏右”?
You can do a normal DFS traversal
and at each node find the max height of the left subtree
and max height of the right subtree
.
Then the tree is balanced if abs(height_left - height_right) <= 1
for all nodes.
To find the type of unbalanced tree (by which i think you mean the type of rotation that is requires to fix the tree), you can use left and right child pointers to deduce the type of rotation is required.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.