-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve 110 - Balanced Binary tree (#20)
- Loading branch information
1 parent
ca7f221
commit d1a9086
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
lib/solutions/00110_balanced_binary_tree/balanced_binary_tree.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
70 changes: 70 additions & 0 deletions
70
test/solutions/00110_balanced_binary_tree/balanced_binary_tree_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |