diff --git a/trees/README.md b/trees/README.md index 884f1e8..40c3628 100644 --- a/trees/README.md +++ b/trees/README.md @@ -66,32 +66,6 @@ def is_leaf(node):
---- - -### balanced trees - -
- -* a **balanced tree** is a binary tree in which the left and right subtrees of every node differ in height by no more than 1. - -
- -```python -def height(root): - if root is None: - return -1 - - return 1 + max(height(root.left), height(root.right)) - -def is_balanced(root): - if root is None: - return True - - return abs(height(root.left) - height(root.right)) < 2 and \ - is_balanced(root.left) and is_balanced(root.right) -``` - -
---- @@ -135,6 +109,35 @@ def height(root): return 1 + max(height(root.left), height(root.right)) ``` +
+ +--- + +### balanced trees + +
+ +* a **balanced tree** is a binary tree in which the left and right subtrees of every node differ in height by no more than 1. + +
+ +```python +def height(root): + if root is None: + return -1 + + return 1 + max(height(root.left), height(root.right)) + +def is_balanced(root): + if root is None: + return True + + return abs(height(root.left) - height(root.right)) < 2 and \ + is_balanced(root.left) and is_balanced(root.right) +``` + +
+ --- ### tree traversal: breath-first search (level-order)