From d098e67ffd29cf70c2091c88e05ca9566f6a5338 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Wed, 5 Aug 2015 18:15:03 -0400 Subject: [PATCH] Saved tree height challenge solution so it can be reused as a component to other related challenges. --- graphs_trees/tree_height/height.py | 5 ++++ .../tree_height/height_solution.ipynb | 26 ++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 graphs_trees/tree_height/height.py diff --git a/graphs_trees/tree_height/height.py b/graphs_trees/tree_height/height.py new file mode 100644 index 0000000..581900b --- /dev/null +++ b/graphs_trees/tree_height/height.py @@ -0,0 +1,5 @@ +def height(node): + if node is None: + return 0 + return 1 + max(height(node.left), + height(node.right)) \ No newline at end of file diff --git a/graphs_trees/tree_height/height_solution.ipynb b/graphs_trees/tree_height/height_solution.ipynb index 07dc71f..42d5b4c 100644 --- a/graphs_trees/tree_height/height_solution.ipynb +++ b/graphs_trees/tree_height/height_solution.ipynb @@ -89,8 +89,17 @@ "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Writing height.py\n" + ] + } + ], "source": [ + "%%writefile height.py\n", "def height(node):\n", " if node is None:\n", " return 0\n", @@ -98,6 +107,17 @@ " height(node.right))" ] }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%run height.py" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -107,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -150,7 +170,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "collapsed": false },