Determine whether a binary tree is balanced

Source: Internet
Author: User
Question: Enter the root node of a binary tree to determine whether the tree is balanced. If the depth difference between left and right Subtrees of any node in a binary tree does not exceed 1, it is a balanced binary tree. Note: whether the binary tree is a binary sorting tree is not considered here.
Solution: 1. Traverse Binary Trees in descending order; 2. recursion.

Core algorithms:

Bool isbalanced (ptree PT, int * depth) {If (! Pt) // parameter judgment {* depth = 0; return true;} // returns int left, right; if (isbalanced (Pt-> lchild, & left) & isbalanced (Pt-> rchild, & right) {int differ = left-right; if (differ> =-1 & differ <= 1) {// record depth * depth = (left> right )? Left + 1: Right + 1; return true;} return false;} bool isbalanced (ptree root) // input the root node of the Binary Tree {int depth = 0; return isbalanced (root, & depth );}

Complete program:

/********************************* Determine whether a binary tree is a balanced binary tree rowandjj2014/7/13 ********************************/# include <iostream> using namespace STD; typedef struct _ node _ {int data; struct _ node _ * lchild; struct _ node _ * rchild;} treenode, * ptree; void create (ptree * PT) {int e; cin> E; If (E! =-1) {* PT = (treenode *) malloc (sizeof (treenode); If (! Pt) {exit (-1) ;}( * PT)-> DATA = E; (* PT)-> lchild = NULL; (* PT)-> rchild = NULL; create (& (* PT)-> lchild); Create (& (* PT)-> rchild) ;}} bool isbalanced (ptree PT, int * depth) {If (! Pt) {* depth = 0; return true;} int left, right; if (isbalanced (Pt-> lchild, & left) & isbalanced (Pt-> rchild, & right) {int differ = left-right; if (differ >=- 1 & differ <= 1) {* depth = (left> right )? Left + 1: Right + 1; return true;} return false;} bool isbalanced (ptree root) // input the root node of the Binary Tree {int depth = 0; return isbalanced (root, & depth);} void travel (ptree pt) {If (PT! = NULL) {travel (Pt-> lchild); travel (Pt-> rchild); cout <Pt-> data <";}} int main () {ptree pt; Create (& pt); travel (PT); cout <Endl; cout <isbalanced (PT); Return 0 ;}




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.