forked from keon/algorithms
-
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.
* Fix bug: explicity compare with None. * Unify node class * Hide slower code. * Update * Only test when file is main * Remove unnecessary code. * Rename and hide implementations. * Fix flake8 * Fix error. * Fix error. * Create __init__.py * Fix error.
- Loading branch information
Showing
21 changed files
with
143 additions
and
158 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 @@ | ||
|
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
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
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 |
---|---|---|
@@ -1,33 +1,36 @@ | ||
|
||
def is_balanced(root): | ||
return __is_balanced_recursive(root) | ||
|
||
|
||
def __is_balanced_recursive(root): | ||
""" | ||
O(N) solution | ||
""" | ||
return -1 != get_depth(root) | ||
return -1 != __get_depth(root) | ||
|
||
|
||
def get_depth(root): | ||
def __get_depth(root): | ||
""" | ||
return 0 if unbalanced else depth + 1 | ||
""" | ||
if not root: | ||
if root is None: | ||
return 0 | ||
left = get_depth(root.left) | ||
right = get_depth(root.right) | ||
if abs(left-right) > 1 or left == -1 or right == -1: | ||
left = __get_depth(root.left) | ||
right = __get_depth(root.right) | ||
if abs(left-right) > 1 or -1 in [left, right]: | ||
return -1 | ||
return 1 + max(left, right) | ||
|
||
################################ | ||
|
||
def is_balanced(root): | ||
""" | ||
O(N^2) solution | ||
""" | ||
left = max_height(root.left) | ||
right = max_height(root.right) | ||
return abs(left-right) <= 1 and is_balanced(root.left) and is_balanced(root.right) | ||
# def is_balanced(root): | ||
# """ | ||
# O(N^2) solution | ||
# """ | ||
# left = max_height(root.left) | ||
# right = max_height(root.right) | ||
# return abs(left-right) <= 1 and is_balanced(root.left) and is_balanced(root.right) | ||
|
||
def max_height(root): | ||
if not root: | ||
return 0 | ||
return max(max_height(root.left), max_height(root.right)) + 1 | ||
# def max_height(root): | ||
# if root is None: | ||
# return 0 | ||
# return max(max_height(root.left), max_height(root.right)) + 1 |
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
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
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
Oops, something went wrong.