"<small><i>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).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Implement depth-first searches (in-order, pre-order, post-order traversals) on a binary tree.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Can we assume we already have a Node class with an insert method?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"### In-Order Traversal\n",
"\n",
"* 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8\n",
"* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5\n",
"\n",
"### Pre-Order Traversal\n",
"\n",
"* 5, 2, 8, 1, 3 -> 5, 2, 1, 3, 8\n",
"* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5\n",
"\n",
"### Post-Order Traversal\n",
"\n",
"* 5, 2, 8, 1, 3 -> 1, 3, 2, 8, 5\n",
"* 1, 2, 3, 4, 5 -> 5, 4, 3, 2, 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"## Test Cases\n",
"\n",
"### In-Order Traversal\n",
"\n",
"* Recursively call in-order traversal on the left child\n",
"* Visit the current node\n",
"* Recursively call in-order traversal on the right child\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: O(log n)\n",
"\n",
"Note:\n",
"* This is a form of a depth-first traversal\n",
"\n",
"### Pre-Order Traversal\n",
"\n",
"* Visit the current node\n",
"* Recursively call pre-order traversal on the left child\n",
"* Recursively call pre-order traversal on the right child\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: O(log n)\n",
"\n",
"Note:\n",
"* This is a form of a depth-first traversal\n",
"\n",
"### Post-Order Traversal\n",
"\n",
"* Recursively call post-order traversal on the left child\n",
"* Recursively call post-order traversal on the right child\n",