Leetcode 110. Balanced binary tree Balanced binary trees

Source: Internet
Author: User

Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree was defined as a binary tree in which the depth of the Every node never differ by more than 1.

Main topic:

Determine if a binary tree is a balanced binary tree.

Ideas:

    1. Do an auxiliary function to find the height of the tree.

    2. Recursive solution through auxiliary functions.


The code is as follows:

/** * definition for a binary tree node. * struct treenode  { *     int val; *     treenode * Left; *     treenode *right; *     treenode ( INT&NBSP;X)  : val (x),  left (null),  right (null)  {} * }; */class  Solution {public:    int depth (treenode* root)     {         if (!root)              return 0;        int l =  Depth (Root->left)  ;        int r = depth (root- >right)  ;        return 1 +  (l >  R) l:r);    &nBSP;}     bool isbalanced (treenode* root)  {         if (!root)             return true;         else        {             int l = depth (Root->left);             int r = depth (root-> right);             if (l + 1 <  r | | &NBSP;R&NBSP;+&NBSP;1&NBSP;&LT;L)             {                 return  false;            }     & nbsp;      else                 return  (isbalanced (root->left)  && isbalanced (root-> right)  );        }    }};


2016-08-08 00:25:26

This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1835477

Leetcode 110. Balanced binary tree Balanced binary trees

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.