Balanced Binary Tree [LeetCode]

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Solution: 

     bool helperBalanced(int *depth, TreeNode * root){
if(root->left == NULL && root->right == NULL){
(*depth) = ;
return true;
} int left_depth = ;
int right_depth = ;
if(root->left != NULL){
if(helperBalanced(&left_depth, root->left) == false)
return false;
} if(root->right != NULL){
if(helperBalanced(&right_depth, root->right) == false)
return false;
} if(abs(right_depth - left_depth) > )
return false; (*depth) = max(left_depth, right_depth) + ; return true;
} bool isBalanced(TreeNode *root) {
if(root == NULL)
return true;
int depth= ;
return helperBalanced(&depth, root);
}
上一篇:配置ssh连接会话复用免密码登录


下一篇:三种方法实现CSS三栏布局