But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a **free** Interview Cake problem every week.

**
You're in!
**

**
Write a method to see if a binary tree is "superbalanced" (a new tree property we just made up).
**

A tree is "superbalanced" if the difference between the depths of any two leaf nodes is no greater than one.

Here's a sample binary tree node class:

public class BinaryTreeNode {
public int value;
public BinaryTreeNode left;
public BinaryTreeNode right;
public BinaryTreeNode(int value) {
this.value = value;
}
public BinaryTreeNode insertLeft(int leftValue) {
this.left = new BinaryTreeNode(leftValue);
return this.left;
}
public BinaryTreeNode insertRight(int rightValue) {
this.right = new BinaryTreeNode(rightValue);
return this.right;
}
}

Your first thought might be to write a recursive method, thinking, "the tree is balanced if the left subtree is balanced and the right subtree is balanced." This kind of approach works well for some other tree problems.

**But this isn't quite true**. Counterexample: suppose that from the root of our tree:

- The left subtree only has leaves at depths 10 and 11.
- The right subtree only has leaves at depths 11 and 12.

Both subtrees are balanced, but from the root we will have leaves at 3 different depths.

We could instead have our recursive method get the array of distinct leaf depths for each subtree. That could work fine. But let's come up with an iterative solution instead. It's usually better to use an iterative solution instead of a recursive one because it avoids stack overflow.

We can do this in time and space.

What about a tree with only one leaf node? Does your method handle that case properly?

We'll never post on your wall or message your friends.

Once you're logged in, you'll get free full access to this and 4 other questions.

We'll never post on your wall or message your friends.

Once you're logged in, you'll get free full access to this and 4 other questions.

time and space.

For time, the worst case is the tree *is* balanced and we have to iterate over *all n nodes* to make sure.

For the space cost, we have two data structures to watch: depths and nodes.

depths will never hold more than three elements, so we can write that off as .

Because we’re doing a depth first search, nodes will hold at most d nodes where d is the depth of the tree (the number of levels in the tree from the root node down to the lowest node). So we *could* say our space cost is .

But we can also relate d to n. In a balanced tree, d is . And the *more unbalanced* the tree gets, the closer d gets to n.

In the worst case, the tree is a straight line of right children from the root where every node in that line also has a left child. The traversal will walk down the line of right children, adding a new left child to nodes at each step. When the traversal hits the rightmost node, nodes will hold *half* of the n total nodes in the tree. Half $n$ is , so our worst case space cost is .

We'll never post on your wall or message your friends.

Once you're logged in, you'll get free full access to this and 4 other questions.

Powered by qualified.io

{"id":7756985,"username":"2018-03-21_18:30:59_d$hzj6","email":null,"date_joined":"2018-03-21T18:30:59.030975+00:00","first_name":"","last_name":"","full_name":"","short_name":"friend","is_anonymous":true,"is_on_last_question":false,"percent_done":0,"num_questions_done":0,"num_questions_remaining":46,"recruiting_is_interested_in_intros":null,"is_full_access":false,"first_payment_date":null,"last_payment_date":null,"num_free_questions_left":3,"terms_has_agreed_to_latest":false,"preferred_content_language":"","preferred_editor_language":"","is_staff":false,"auth_providers_human_readable_list":"","num_auth_providers":0,"auth_email":"","profile_public_id":null}

. . .