Determine whether a binary tree is an AVL Tree

Source: Internet
Author: User

Idea: the AVL Tree is a highly balanced binary search tree. Here, we want to clearly describe whether it is a search tree or a Balance Tree.

Struct treenode {struct treenode * left; struct treenode * right; int key ;}; // determine whether it is a binary search tree, second, determine whether it is a balanced bool isavl (treenode * root, int depth) {If (isbst (Root) & isbalance (root, & depth) return true; return false ;} // determine whether it is a binary search tree bool isbst (treenode * root) {If (root = NULL) return true; If (! Isbst (root-> left) return false; If (! Isbst (root-> right) return false; treenode * cur = root-> left; If (cur! = NULL) {While (cur-> right! = NULL) cur = cur-> right; if (root-> key <cur-> key) return false;} treenode * cur = root-> right; if (cur! = NULL) {While (cur-> left! = NULL) cur = cur-> left; If (root-> key> cur-> key) return false;} return true ;} // determine whether bool isbalance (treenode * root, int * depth) {If (root = NULL) {* depth = 0; return true;} int depthl, depthr; if (! Isbalance (root-> left, & depthl) return false; If (! Isbalance (root-> right, & depthr) return false; int diff = depthl-depthr; If (diff> 1 | diff <-1) return false; * depth = 1 + (depthl> depthr? Depthl: depthr); Return true ;}

 

Determine whether a binary tree is an AVL Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.