Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Check Binary Tree Is Balanced

Medium Acceptance 78.95% Points 30.00

You are given the root of a binary tree. Your task is to determine whether the binary tree is height-balanced.

A binary tree is considered balanced if:

  • For every node, the difference between the heights of its left and right subtrees is at most 1.
  • Both the left and right subtrees are themselves balanced.

Return true if the tree is balanced; otherwise, return false.

Examples
Example 1

image Input: root = [3,9,20,null,null,15,7]
Output: true
Explanation: The height difference between the left and right subtree of every node is at most one.

Example 2

image Input: root = [1,2,2,3,3,null,null,4,4]
Output: false
Explanation: The left subtree is deeper than the right subtree by more than one level.

Example 3

image Input: root = []
Output: true
Explanation: An empty tree is balanced.

Constraints
    • 0 = Number of nodes = 105
    • -10? = Node.data = 10?
Companies
Apple Deloitte Sprinklr
Topics
Tree Traversal Binary Tree
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions