"Leetcode-Interview algorithm classic-java implementation" "110-balanced binary tree (balanced binary trees)"

Source: Internet
Author: User

"110-balanced binary tree (balanced binary trees)""leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"Original Question

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 subtrees of the Y node never differ by more than 1.

Main Topic

Given a balanced binary tree, determine whether it is highly balanced. A highly balanced two-prong tree is not more than 1 of the height of the left and right sub-trees, and so is the tree.

Thinking of solving problems

The method is solved by recursive division and treatment.

Code Implementation

publicclass TreeNode {    int val;    TreeNode left;    TreeNode right;    TreeNode(int x) { val = x; }}

Algorithm implementation class

 Public  class solution {    Private intMindepth =0;Private intMaxDepth =0; Public Boolean isbalanced(TreeNode Root) {if(Root = =NULL) {return true; }intleft = depth (root.left);intright = depth (root.right);if(Left-right >1|| Left-right <-1) {return false; }Else{returnisbalanced (Root.left) && isbalanced (root.right); }    }/** * Find the height of the tree * @param The root node of the N-tree * @return The height of the tree * *    Private int Depth(TreeNode N) {if(n = =NULL) {return 0; }if(N.left = =NULL&& N.right = =NULL) {return 1; }Else{intleft = depth (n.left);intright = depth (n.right);return 1+ (Left > right left:right); }    }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special InstructionsWelcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47414041"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java implementation" "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.