-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
122 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
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,47 @@ | ||
package day247 | ||
|
||
// BinaryTree is a simple binary tree without a value. | ||
type BinaryTree struct { | ||
// Value int // is unnecessary. We don't need any value. | ||
Left, Right *BinaryTree | ||
} | ||
|
||
// IsHeightBalanced returns true if the tree is height balanced. | ||
// It returns false otherwise. | ||
// Runs in O(N) time. | ||
func IsHeightBalanced(root *BinaryTree) bool { | ||
_, balanced := isHeightBalanced(root) | ||
return balanced | ||
} | ||
|
||
func isHeightBalanced(n *BinaryTree) (int, bool) { | ||
if n == nil { | ||
return 0, true | ||
} | ||
leftHeight, leftBalance := isHeightBalanced(n.Left) | ||
if !leftBalance { | ||
return 0, false | ||
} | ||
rightHeight, rightBalance := isHeightBalanced(n.Right) | ||
if !rightBalance { | ||
return 0, false | ||
} | ||
if diff := abs(leftHeight - rightHeight); diff > 1 { | ||
return 0, false | ||
} | ||
return max(leftHeight, rightHeight) + 1, true | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
func abs(a int) int { | ||
if a < 0 { | ||
return -a | ||
} | ||
return a | ||
} |
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,74 @@ | ||
package day247 | ||
|
||
import "testing" | ||
|
||
// nolint | ||
var testcases = []struct { | ||
root *BinaryTree | ||
heightBalanced bool | ||
}{ | ||
{ | ||
&BinaryTree{ | ||
&BinaryTree{nil, nil}, | ||
&BinaryTree{nil, nil}, | ||
}, | ||
true, | ||
}, | ||
{ | ||
&BinaryTree{ | ||
nil, | ||
&BinaryTree{nil, nil}, | ||
}, | ||
true, | ||
}, | ||
{ | ||
&BinaryTree{ | ||
nil, | ||
&BinaryTree{&BinaryTree{nil, nil}, nil}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
&BinaryTree{ | ||
nil, | ||
&BinaryTree{ | ||
&BinaryTree{ | ||
nil, | ||
&BinaryTree{nil, nil}, | ||
}, | ||
nil, | ||
}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
&BinaryTree{ | ||
&BinaryTree{ | ||
nil, | ||
&BinaryTree{ | ||
&BinaryTree{nil, nil}, | ||
nil, | ||
}, | ||
}, | ||
nil, | ||
}, | ||
false, | ||
}, | ||
} | ||
|
||
func TestIsHeightBalanced(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := IsHeightBalanced(tc.root); result != tc.heightBalanced { | ||
t.Errorf("expected %v, got %v", tc.heightBalanced, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkIsHeightBalanced(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
IsHeightBalanced(tc.root) | ||
} | ||
} | ||
} |