From 92a74485ec0882b9c6266b1ac75b2ef10629fef5 Mon Sep 17 00:00:00 2001 From: bt3gl <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Tue, 8 Aug 2023 16:09:42 -0700 Subject: [PATCH] Update README.md --- trees/README.md | 55 ++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 26 deletions(-) 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)