bool isBalanced(TreeNode *node, int& treeHeight) { if (!node) { treeHeight = 0; return true; } if (!node->left && !node->right) { treeHeight = 1; return true; } int leftHeight = 0; bool lb = isBalanced(node->left, leftHeight); int rightHeight = 0; bool rb = isBalanced(node->right, rightHeight); treeHeight = max(leftHeight, rightHeight) + 1; if (lb && rb && abs(leftHeight - rightHeight) <= 1) { return true; } return false; }
/* The function returns true if root is balanced else false The second parameter is to store the height of tree. Initially, we need to pass a pointer to a location with value as 0. We can also write a wrapper over this function */ bool isBalanced(struct node *root, int* height) { /* lh --> Height of left subtree rh --> Height of right subtree */ int lh = 0, rh = 0; /* l will be true if left subtree is balanced and r will be true if right subtree is balanced */ int l = 0, r = 0; if(root == NULL) { *height = 0; return 1; } /* Get the heights of left and right subtrees in lh and rh And store the returned values in l and r */ l = isBalanced(root->left, &lh); r = isBalanced(root->right,&rh); /* Height of current node is max of heights of left and right subtrees plus 1*/ *height = (lh > rh? lh: rh) + 1; /* If difference between heights of left and right subtrees is more than 2 then this node is not balanced so return 0 */ if((lh - rh >= 2) || (rh - lh >= 2)) return 0; /* If this node is balanced and left and right subtrees are balanced then return true */ else return l&&r; }
bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return true; return isBalanced(root->left) && isBalanced(root->right) && abs(height(root->left) - height(root->right)) <= 1; }
bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if (root == NULL) return true; // determine the left & right tree are ballanced // bool result = isBalanced( root-> left) && isBalanced(root->right); int left_depth, right_depth; // get the depth of left and right sub-tree if (root->left != NULL) left_depth = root -> left -> val; else left_depth = 0; if (root -> right != NULL ) right_depth = root -> right -> val; else right_depth = 0; // determine the depth of root root -> val = right_depth + 1; if (left_depth > right_depth) root -> val = left_depth + 1; result = ( result && (right_depth - left_depth > -2) && (right_depth - left_depth < 2 )); return result; }
bool isBalanced(TreeNode *root) { function<int (TreeNode*)> getheight = [&getheight] (TreeNode* node) -> int { if(node == nullptr){ return 0; } if(node->left == nullptr and node->right == nullptr){ return 1; } unsigned left_height = 0; unsigned right_height = 0; if(node->left != nullptr){ left_height = 1 + getheight(node->left); } if(node->right != nullptr){ right_height = 1 + getheight(node->right); } return std::max(left_height,right_height); }; int lhs,rhs; if(root == nullptr){ return true; } else if(root->left == nullptr and root->right == nullptr){ return true; } else{ lhs = getheight(root->left); rhs = getheight(root->right); } return ((std::abs(lhs - rhs) <= 1) and isBalanced(root->left) and isBalanced(root->right)); }
bool isBalanced(TreeNode* root) { if(root == NULL) return true; if(root->left == NULL && root->right == NULL){ root->val = 1; return true; } else if(root->left == NULL){ if(root->right->left == NULL && root->right->right == NULL){ root->val = 2; return true; } } else if(root->right == NULL){ if(root->left->right == NULL && root->left->left == NULL){ root->val = 2; return true; } } else { if(isBalanced(root->left) && isBalanced(root->right)){ root->val = max(root->left->val, root->right->val) + 1; if(abs(root->left->val - root->right->val) < 2)return true; } } return false; }
int isBalanced(struct TreeNode* root) { if(root == NULL) return 1; int temp = depthTree(root->right) - depthTree(root->left); if(temp < -1 || temp > 1) return 0; else return isBalanced(root->left) && isBalanced(root->right); }
bool isBalanced(struct TreeNode* root) { bool lb, rb; if(!root) return true; lb = isBalanced(root->left); rb = isBalanced(root->right); return (lb && rb && (abs(Height(root->left) - Height(root->right)) < 2)); }
bool isBalanced(TreeNode *root) { if (!root) return true; int val = depth(root->left) - depth(root->right); if (abs(val) <= 1) { return isBalanced(root->left) && isBalanced(root->right); } return false; }
bool isBalanced(TreeNode* root) { if (root == NULL) return true; if (abs(calHeight(root->left) - calHeight(root->right)) <= 1){ return (isBalanced(root->left) && isBalanced(root->right)); } else return false; }
bool isBalanced(TreeNode* root) { if (!root) { return true; } int lh = 0; int rh = 0; getHeight(root->left, 1, lh); getHeight(root->right, 1, rh); return (lh - rh < 2 && lh - rh > -2 && isBalanced(root->left) && isBalanced(root->right)); }
bool isBalanced(TreeNode* root) { if( root == NULL ) return true; if( abs(get_height(root->left)-get_height(root->right)) > 1){ return false; } return isBalanced(root->left) && isBalanced(root->right); }
bool isBalanced(TreeNode* root) { if (!root) return true; if (isBalanced(root->left) && isBalanced(root->right) && my_abs(GetHeight(root->left) - GetHeight(root->right)) <= 1) { return true; } return false; }
bool isBalanced(TreeNode *root) { if(root==NULL) return true; int leftHeight = maxDepth(root->left); int rightHeight = maxDepth(root->right); return abs(leftHeight-rightHeight)<=1 && isBalanced(root->left) && isBalanced(root->right); }
bool isBalanced(TreeNode *root) { if (!root) //Note: an empty tree is a blanced tree return true; int left = maxDepth(root->left), right = maxDepth(root->right); return abs(left - right) <= 1 && isBalanced(root->left) && isBalanced(root->right); }
bool isBalanced(TreeNode* root) { if (!root) return true; int leftHeight = getHeight(root->left); int rightHeight = getHeight(root->right); return (abs(leftHeight-rightHeight) <= 1 && isBalanced(root->left) && isBalanced(root->right)); }
bool isBalanced(TreeNode* root) { if (!root) { return true; } if (abs(treeDepth(root->left) - treeDepth(root->right)) <= 1) { return isBalanced(root->left) && isBalanced(root->right); } return false; }
bool isBalanced(TreeNode *root) { if(root == NULL) return true; if(root->left == NULL && root->right == NULL) return true; int dif = abs(depth(root->left) - depth(root->right)); return isBalanced(root->left) && isBalanced(root->right) && dif<=1; }
bool isBalanced(TreeNode* root) { if (!root){ return true; } return isBalanced(root->left) && isBalanced(root->right) && abs(getHeight(root->left)-getHeight(root->right))<=1; }
int isBalanced(treeElem* root) { if (root == 0) return 1; else { return isBalanced(root->left)&&isBalanced(root->right)&&(abs(MaxDepth(root->left)-MaxDepth(root->right)) <= 1); } }
bool isBalanced(TreeNode* root) { if(root == NULL) return true; if(!isBalanced(root->left) || !isBalanced(root->right)) return false; return (abs(depthOfTree(root->left)-depthOfTree(root->right)) <= 1); }
bool isBalanced(TreeNode* root) { if (!root) return true; int left_depth = depth(root->left); int right_depth = depth(root->right); return abs(left_depth - right_depth) <= 1 && isBalanced(root->left) && isBalanced(root->right); }
bool isBalanced(TreeNode *root) { if (root == NULL) return true; else { int l = height(root->left); int r = height(root->right); if (l - r >= 2 || l - r <= -2) return false; return isBalanced(root->left) && isBalanced(root->right); } }
bool isBalanced(TreeNode* root) { if(root == NULL) return true; int lh = treeHeight(root->left); int rh = treeHeight(root->right); if(lh - rh > 1 or lh - rh < -1) return false; return isBalanced(root->left) && isBalanced(root->right); }
bool isBalanced(TreeNode* root) { if (!root) return true; if ( abs( maxDepth(root->left) - maxDepth(root->right)) > 1 || !isBalanced(root->left) || !isBalanced(root->right) ) return false; return true; }
bool isBalanced(TreeNode *root, int &depth) { if (!root) { return true; } int leftDepth = 0, rightDepth = 0; bool leftBalanced = isBalanced(root->left, leftDepth); bool rightBalanced = isBalanced(root->right, rightDepth); depth = leftDepth > rightDepth? leftDepth + 1 : rightDepth + 1; return leftBalanced && rightBalanced && abs(leftDepth - rightDepth) < 2; }
bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return true; int diff = abs(maxHeight(root->left) - maxHeight(root->right)); if(diff > 1) return false; return isBalanced(root->left) && isBalanced(root->right); }
bool isBalanced(node *root){ if(root == NULL) return true; int left = maxDepth(root->left); int right = maxDepth(root->right); if(abs(left-right) < 2) return isBalanced(root->left) && isBalanced(root->right); else return false; }
bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root==NULL) return true; if(abs(maxh(root->right)-maxh(root->left)) > 1) return 0; else return isBalanced(root->right) && isBalanced(root->left); }
/** * 给定一个二叉树,确定它是高度平衡的。对于这个问题,一棵高度平衡的二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过1。时间复杂度O(n) 空间复杂度O(n) * * @param root 需要判断的二叉树 * * @return 平衡返回true */ bool isBalanced(TreeNode *root) { if (root == NULL) { return true; } // write your code here if (abs(maxDepth(root->right)-maxDepth(root->left)) <= 1) { return isBalanced(root->right) && isBalanced(root->left); } else { return false; } }