Title Description
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.
Ideas
Judging whether a tree is a balanced binary tree, recursively seeking the tree height of the Saozi right subtree of each node, judging the difference of the height of the Saozi right sub-tree not exceeding 1
Code implementation
classsolution{ Public: bool Isbalanced (TreeNode *root) {return balancedheight (root) >=0; }intBalancedheight (treenode* root) {if(root==NULL) return0;int Left=balancedheight (root-> Left);int Right=balancedheight (root-> Right);if( Left<0|| Right<0||ABS( Left- Right) >1) Return-1; Return Max ( Left, Right)+1; } };
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode Balanced Binary Tree