Skip to content

Commit d1a9086

Browse files
authored
Solve 110 - Balanced Binary tree (#20)
1 parent ca7f221 commit d1a9086

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Balanced Binary Tree
2+
3+
**Link to Problem**: https://leetcode.com/problems/balanced-binary-tree
4+
5+
## Description
6+
7+
Given a binary tree, determine if it is height-balanced.
8+
9+
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.
10+
11+
## Examples
12+
13+
### Example 1
14+
15+
```mermaid
16+
graph
17+
A((3)) --> B((9))
18+
A --> C((20))
19+
C --> D((15))
20+
C --> E((7))
21+
```
22+
23+
```
24+
Input: root = [3,9,20,null,null,15,7]
25+
Output: true
26+
```
27+
28+
## Example 2
29+
30+
```mermaid
31+
graph
32+
A((1)) --> B((2))
33+
A --> C((2))
34+
B --> D((3))
35+
B --> E((3))
36+
D --> F((4))
37+
D --> G((4))
38+
```
39+
40+
```
41+
Input: root = [1,2,2,3,3,null,null,4,4]
42+
Output: false
43+
```
44+
45+
## Example 3
46+
47+
```
48+
Input: root = []
49+
Output: true
50+
```
51+
52+
## Thoughts
53+
54+
I'm beginning to hate trees and recursion. I'm still struggling with these things even though
55+
I understand how the algorithm should work. For some reason, I have a hard time translating
56+
that understanding into code.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmodule LeetCodePractice.Solutions.BalancedBinaryTree do
2+
@moduledoc """
3+
Given a binary tree, determine if it is height-balanced.
4+
5+
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.
6+
"""
7+
8+
alias LeetCodePractice.Provisions.TreeNode
9+
10+
@spec call(root :: TreeNode.t() | nil) :: boolean
11+
def call(root) do
12+
match?({:ok, _}, do_is_balanced(root))
13+
end
14+
15+
defp do_is_balanced(nil), do: {:ok, 0}
16+
17+
defp do_is_balanced(root) do
18+
with {:ok, left_height} <- do_is_balanced(root.left),
19+
{:ok, right_height} <- do_is_balanced(root.right),
20+
true <- abs(left_height - right_height) <= 1 do
21+
{:ok, 1 + max(left_height, right_height)}
22+
end
23+
end
24+
end
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
defmodule LeetCodePractice.Solutions.BalancedBinaryTreeTest do
2+
use ExUnit.Case, async: true
3+
4+
alias LeetCodePractice.Provisions.TreeNode
5+
alias LeetCodePractice.Solutions.BalancedBinaryTree
6+
7+
test "Case 1 works" do
8+
root = %TreeNode{
9+
val: 3,
10+
left: %TreeNode{
11+
val: 9,
12+
left: nil,
13+
right: nil
14+
},
15+
right: %TreeNode{
16+
val: 20,
17+
left: %TreeNode{
18+
val: 15,
19+
left: nil,
20+
right: nil
21+
},
22+
right: %TreeNode{
23+
val: 7,
24+
left: nil,
25+
right: nil
26+
}
27+
}
28+
}
29+
30+
assert BalancedBinaryTree.call(root) == true
31+
end
32+
33+
test "Case 2 works" do
34+
root = %TreeNode{
35+
val: 1,
36+
left: %TreeNode{
37+
val: 2,
38+
left: %TreeNode{
39+
val: 3,
40+
left: %TreeNode{
41+
val: 4,
42+
left: nil,
43+
right: nil
44+
},
45+
right: %TreeNode{
46+
val: 4,
47+
left: nil,
48+
right: nil
49+
}
50+
},
51+
right: %TreeNode{
52+
val: 3,
53+
left: nil,
54+
right: nil
55+
}
56+
},
57+
right: %TreeNode{
58+
val: 2,
59+
left: nil,
60+
right: nil
61+
}
62+
}
63+
64+
assert BalancedBinaryTree.call(root) == false
65+
end
66+
67+
test "Case 3 works" do
68+
assert BalancedBinaryTree.call(nil) == true
69+
end
70+
end

0 commit comments

Comments
 (0)