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.
The depth of the node is calculated for each node, and the depth difference between the left and right nodes is greater than 1,false.
1 /**2 * Definition for a binary tree node.3 * struct TreeNode {4 * int val;5 * TreeNode *left;6 * TreeNode *right;7 * TreeNode (int x): Val (x), left (null), right (null) {}8 * };9 */Ten classSolution { One Public: A intDepthoftree (treenode*root) - { - if(!root)return 0; the returnMax (Depthoftree (Root->left), Depthoftree (root->right)) +1; - } - BOOLIsbalanced (treenode*root) { - if(!root)return true; + if(ABS (Depthoftree (Root->left)-depthoftree (root->right)) >1)return false; - BOOLleft = isbalanced (root->Left ); + BOOLright = isbalanced (root->Right ); A returnleft&&Right ; at } -};
[Leetcode] Balanced Binary Tree