Skip to content

Commit a4ad9be

Browse files
solves balanced binary tree python
1 parent d26fdf3 commit a4ad9be

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MaximumDepthOfBinaryTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/maximum_depth_of_binary_tree.py) |
3434
| 107 | [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/BinaryTreeLevelOrderTraversalII.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/binary_tree_level_order_traversal_ii.py) |
3535
| 108 | [Convert Sorted Array To Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ConvertSortedArrayToBinarySearchTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/converted_sorted_array_to_binary_search_tree.py) |
36-
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/BalancedBinaryTree.java) |
36+
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/BalancedBinaryTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/balanced_binary_tree.py) |
3737
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MinimumDepthOfBinaryTree.java) |
3838
| 112 | [Path Sum](https://leetcode.com/problems/path-sum) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PathSum.java) |
3939
| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PascalsTriangle.java) |

python/balanced_binary_tree.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
9+
class Solution:
10+
def _is_balanced(self, root: TreeNode) -> int:
11+
if root is None:
12+
return 0
13+
14+
left_height = self._is_balanced(root.left)
15+
right_height = self._is_balanced(root.right)
16+
if left_height == -1 or right_height == -1 or abs(left_height - right_height) > 1:
17+
return -1
18+
19+
return 1 + max(left_height, right_height)
20+
21+
def isBalanced(self, root: TreeNode) -> bool:
22+
if root is None:
23+
return True
24+
return self._is_balanced(root) is not -1

0 commit comments

Comments
 (0)