"Tree" Judge balanced binary tree AVL

Source: Internet
Author: User

1 /***********************************2 https://leetcode.com/problems/balanced-binary-tree/3 @date 2015.5.84 @description5 given a binary tree, determine whether it is a balanced binary tree6 @tags tree, DFS7 8 equilibrium binary tree satisfies the condition:9 1. The height difference of the left and right sub-tree is not greater than 1Ten 2. The left and right sub-trees are balanced binary trees One  A This solution offers two methods: - Solution1. - recursion first calculates the height of the left and right sub-trees, then compares the difference with 1, if ≤1, then judge whether the left and right sub-tree is AVL the  - Solution2. - calculates and returns the height of the left and right subtree, as well as determining whether the left and right subtrees are AVL trees, - this does not need to judge the left and right word height difference is not greater than 1 and then go back to determine whether the left and right sub-tree is AVL tree +  - added search binary tree creation and recursive first order traversal binary tree in test function +  A  at ************************************/ -  -  -#include <iostream> -#include <stdlib.h> -  in using namespacestd; -  to structtreenode{ +     intVal; -TreeNode *Left ; theTreeNode *Right ; *TreeNode (intx): Val (x), left (null), right (null) {} $ };Panax Notoginseng  -  the classsolution{ +  Public: A     BOOLisbalanced (TreeNode *root) { the         //Recursive +         if(!root)return true;//empty trees are also AVL -         intDepthleft = MaxDepth (root->Left ); $         intDepthright = MaxDepth (root->right);//The maximum depth of the left and right sub-tree, i.e. the height of the tree $         if(ABS (Depthleft-depthright) <=1)//the height difference of the left and right sub-tree is not more than 1, one condition -             returnisbalanced (Root->left) && isbalanced (root->right);//The left and right sub-trees are AVL, condition two -         Else the             return false; -     }Wuyi  the     intMaxDepth (TreeNode *root) { -         if(!root)return 0; Wu         return 1+ Max (maxDepth (Root->left), maxDepth (root->Right )); -     } About  $ }; -  -  - classsolution2{ A  Public: +     BOOLisbalanced (TreeNode *root) { the         returnDfsdepth (root)! =-1; -     } $  the     intDfsdepth (TreeNode *root) { the         if(!root)return 0; the         intLeftdepth = Dfsdepth (root->Left ); the         if(Leftdepth = =-1)return-1; -         intRightdepth = Dfsdepth (root->Right ); in         if(Rightdepth = =-1)return-1; the  the         if(ABS (Leftdepth-rightdepth) >1) About             return-1; the         returnMax (leftdepth, rightdepth) +1; the     } the }; +  -TreeNode *insert (TreeNode *root,intdata) { theTreeNode *ptr =Root;BayiTreeNode *tempnode;//The parent node of the inserted node is stored theTreeNode *newnode =NewTreeNode (data); the  -     if(ptr = =NULL) -         returnNewNode; the     Else{ the          while(PTR! =NULL) { theTempnode =ptr; the             if(Ptr->val >=data) { -PTR = ptr->Left ; the}Else{ thePTR = ptr->Right ; the             }94         } the         if(Tempnode->val >=data) { theTempnode->left =NewNode; the}Else{98Tempnode->right =NewNode; About         } -     }101     returnRoot;102 }103 104 //Recursive first-order traversal of binary tree the voidTravpre (TreeNode *root) {106     if(!root)return;107cout << Root->val <<" ";108Travpre (root->Left );109Travpre (root->Right ); the }111  the 113 intMain () { theTreeNode *root =NULL; the     inttemp =0; theCIN >>temp;117      while(Temp! =0){//End with 0 (input 0 terminates)118Root =Insert (root, temp);119CIN >>temp; -}//create a two-pronged tree121 122     //Recursive first-order traversal123 Travpre (root);124  thecout <<Endl;126 Solution2 A;127cout <<a.isbalanced (root); -}

"Tree" Judge balanced binary tree AVL

Related Article

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.