FastPrepBalanced Binary Tree

Balanced Binary Tree

Google logoGoogle● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given the root of a binary tree, determine whether it is height-balanced.

A binary tree is height-balanced when, at every node, the heights of the left and right subtrees differ by at most one.

Function

isBalanced(root: TreeNode) → boolean

Examples

Example 1

root = [3,9,20,null,null,15,7]return = true

Every node's two subtree heights differ by at most one.

Example 2

root = [1,2,2,3,3,null,null,4,4]return = false

The left side contains a node whose subtree heights differ by more than one.

Example 3

root = []return = true

An empty tree is height-balanced.

Constraints

  • The tree contains from 0 to 5000 nodes.
  • -10000 <= node.val <= 10000.

More Google problems

See Google hiring insights
public boolean isBalanced(TreeNode root) {
    // Write your code here.
}
root[3,9,20,null,null,15,7]
expectedtrue
Checking account…