Check if a tree is balanced

1 min read

https://practice.geeksforgeeks.org/problems/check-for-balanced-tree/1

bool balancedUtil(Node *root, int &height) {
  int lh = 0, rh = 0;

  if (root == nullptr) {
    height = 0;
    return 1;
  }

  int l = balancedUtil(root->left, lh);
  int r = balancedUtil(root->right, rh);

  height = max(lh, rh) + 1;

  if (abs(lh - rh) > 1)
    return 0;

  else
    return l && r;
}

bool isBalanced(Node *root) {
  int height = 0;
  return balancedUtil(root, height);
}
Zig-zag traversal of a binary tree
Diagonal traversal of a binary tree