Skip to content

Commit

Permalink
Solve 110 - Balanced Binary tree (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
terenceponce authored Dec 7, 2023
1 parent ca7f221 commit d1a9086
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lib/solutions/00110_balanced_binary_tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Balanced Binary Tree

**Link to Problem**: https://leetcode.com/problems/balanced-binary-tree

## Description

Given a binary tree, determine if it is height-balanced.

A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.

## Examples

### Example 1

```mermaid
graph
A((3)) --> B((9))
A --> C((20))
C --> D((15))
C --> E((7))
```

```
Input: root = [3,9,20,null,null,15,7]
Output: true
```

## Example 2

```mermaid
graph
A((1)) --> B((2))
A --> C((2))
B --> D((3))
B --> E((3))
D --> F((4))
D --> G((4))
```

```
Input: root = [1,2,2,3,3,null,null,4,4]
Output: false
```

## Example 3

```
Input: root = []
Output: true
```

## Thoughts

I'm beginning to hate trees and recursion. I'm still struggling with these things even though
I understand how the algorithm should work. For some reason, I have a hard time translating
that understanding into code.
24 changes: 24 additions & 0 deletions lib/solutions/00110_balanced_binary_tree/balanced_binary_tree.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule LeetCodePractice.Solutions.BalancedBinaryTree do
@moduledoc """
Given a binary tree, determine if it is height-balanced.
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
"""

alias LeetCodePractice.Provisions.TreeNode

@spec call(root :: TreeNode.t() | nil) :: boolean
def call(root) do
match?({:ok, _}, do_is_balanced(root))
end

defp do_is_balanced(nil), do: {:ok, 0}

defp do_is_balanced(root) do
with {:ok, left_height} <- do_is_balanced(root.left),
{:ok, right_height} <- do_is_balanced(root.right),
true <- abs(left_height - right_height) <= 1 do
{:ok, 1 + max(left_height, right_height)}
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
defmodule LeetCodePractice.Solutions.BalancedBinaryTreeTest do
use ExUnit.Case, async: true

alias LeetCodePractice.Provisions.TreeNode
alias LeetCodePractice.Solutions.BalancedBinaryTree

test "Case 1 works" do
root = %TreeNode{
val: 3,
left: %TreeNode{
val: 9,
left: nil,
right: nil
},
right: %TreeNode{
val: 20,
left: %TreeNode{
val: 15,
left: nil,
right: nil
},
right: %TreeNode{
val: 7,
left: nil,
right: nil
}
}
}

assert BalancedBinaryTree.call(root) == true
end

test "Case 2 works" do
root = %TreeNode{
val: 1,
left: %TreeNode{
val: 2,
left: %TreeNode{
val: 3,
left: %TreeNode{
val: 4,
left: nil,
right: nil
},
right: %TreeNode{
val: 4,
left: nil,
right: nil
}
},
right: %TreeNode{
val: 3,
left: nil,
right: nil
}
},
right: %TreeNode{
val: 2,
left: nil,
right: nil
}
}

assert BalancedBinaryTree.call(root) == false
end

test "Case 3 works" do
assert BalancedBinaryTree.call(nil) == true
end
end

0 comments on commit d1a9086

Please sign in to comment.