link:https://leetcode.com/problems/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 subtrees of the Y node never differ by more than 1.
Infer whether a tree belongs to a balanced binary tree
Infer that the left and right node depth of the primary node is poor, assuming it is not in " -1,1". returns FALSE. Otherwise. Continue to infer whether the left and right nodes belong to the balanced binary tree;
Just have to be unsatisfied. It returns false.
Code (c + +):
/** * Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *righ T * TreeNode (int x): Val (x), left (null), right (NULL) {} *}; */Class Solution { Public: boolisbalanced(treenode* Root) {if(Root = NULL)return true;intLdepth = Getdepth (root->left);intRdepth = Getdepth (root->right);if(Ldepth-rdepth >1|| Ldepth-rdepth <-1)return false;returnisbalanced (Root->left) && isbalanced (root->right); }//get node ' s depth intGetdepth (TreeNode *root) {if(Root==null)return 0;intLdepth=0, rdepth=0; Ldepth = Getdepth (root->left); Rdepth = Getdepth (root->right);returnLdepth > Rdepth? ldepth+1: rdepth+1; }};
leetcode[110]-balanced Binary Tree