interactive-coding-challenges/graphs_trees/tree_lca/tree_lca_challenge.ipynb

218 lines
5.4 KiB
Python

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Find the lowest common ancestor in a binary tree.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)\n",
"* [Solution Notebook](#Solution-Notebook)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Is this a binary search tree?\n",
" * No\n",
"* Can we assume the two nodes are in the tree?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"<pre>\n",
" _10_\n",
" / \\\n",
" 5 9\n",
" / \\ / \\\n",
" 12 3 18 20\n",
" / \\ /\n",
" 1 8 40\n",
"</pre>\n",
"\n",
"* 0, 5 -> None\n",
"* 5, 0 -> None\n",
"* 1, 8 -> 3\n",
"* 12, 8 -> 5\n",
"* 12, 40 -> 10\n",
"* 9, 20 -> 9\n",
"* 3, 5 -> 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [Solution Notebook](http://nbviewer.jupyter.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_lca/tree_lca_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Node(object):\n",
"\n",
" def __init__(self, key, left=None, right=None):\n",
" self.key = key\n",
" self.left = left\n",
" self.right = right\n",
"\n",
" def __repr__(self):\n",
" return str(self.key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class BinaryTree(object):\n",
"\n",
" def lca(self, root, node1, node2):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %load test_lca.py\n",
"import unittest\n",
"\n",
"\n",
"class TestLowestCommonAncestor(unittest.TestCase):\n",
"\n",
" def test_lca(self):\n",
" node10 = Node(10)\n",
" node5 = Node(5)\n",
" node12 = Node(12)\n",
" node3 = Node(3)\n",
" node1 = Node(1)\n",
" node8 = Node(8)\n",
" node9 = Node(9)\n",
" node18 = Node(18)\n",
" node20 = Node(20)\n",
" node40 = Node(40)\n",
" node3.left = node1\n",
" node3.right = node8\n",
" node5.left = node12\n",
" node5.right = node3\n",
" node20.left = node40\n",
" node9.left = node18\n",
" node9.right = node20\n",
" node10.left = node5\n",
" node10.right = node9\n",
" root = node10\n",
" node0 = Node(0)\n",
" binary_tree = BinaryTree()\n",
" self.assertEqual(binary_tree.lca(root, node0, node5), None)\n",
" self.assertEqual(binary_tree.lca(root, node5, node0), None)\n",
" self.assertEqual(binary_tree.lca(root, node1, node8), node3)\n",
" self.assertEqual(binary_tree.lca(root, node12, node8), node5)\n",
" self.assertEqual(binary_tree.lca(root, node12, node40), node10)\n",
" self.assertEqual(binary_tree.lca(root, node9, node20), node9)\n",
" self.assertEqual(binary_tree.lca(root, node3, node5), node5)\n",
" print('Success: test_lca')\n",
"\n",
"\n",
"def main():\n",
" test = TestLowestCommonAncestor()\n",
" test.test_lca()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}