Lintcode_93 _ balanced binary tree and lintcode_93 Binary Tree

Source: Internet
Author: User

Lintcode_93 _ balanced binary tree and lintcode_93 Binary Tree
Balanced Binary Tree

  • Description
  • Notes
  • Data
  • Evaluation

Given a binary tree, make sure it is highly balanced. For this problem, a highly balanced binary tree is defined as: the depth difference between the two Subtrees of each node in a binary tree does not exceed 1.

Have you ever encountered this question during a real interview? Yes Example

Returns the binary tree A ={3,9,20,#,#,15,7}, B ={3,#,20,15,7}

A)  3            B)    3    / \                  \  9  20                 20    /  \                / \   15   7              15  7

Binary Tree A is A highly balanced binary tree, but B is not

Tag  Related Questions
/** * Definition of TreeNode: * class TreeNode { * public: *     int val; *     TreeNode *left, *right; *     TreeNode(int val) { *         this->val = val; *         this->left = this->right = NULL; *     } * } */class Solution {public:    /*     * @param root: The root of binary tree.     * @return: True if this Binary tree is Balanced, or false.     */    int judge(TreeNode *root) {        // write your code here        if(root==NULL)            return 0;        int l=judge(root->left);        int r=judge(root->right);        if(l==-1||r==-1||fabs(l-r)>1)            return -1;        return max(l,r)+1;    }    bool isBalanced(TreeNode * root) {        // write your code here        if(root==NULL)            return 1;        return judge(root)>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.