"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)"