mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-10 11:28:03 +00:00
Polish tree dfs challenge and solution (#78)
Update constraints and algorithm discussion.
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Implement depth-first searches (in-order, pre-order, post-order traversals) on a binary tree.\n",
|
"## Problem: Implement depth-first traversals (in-order, pre-order, post-order) on a binary tree.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* [Constraints](#Constraints)\n",
|
"* [Constraints](#Constraints)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
@@ -34,6 +34,10 @@
|
|||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Can we assume we already have a Node class with an insert method?\n",
|
"* Can we assume we already have a Node class with an insert method?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* What should we do with each node when we process it?\n",
|
||||||
|
" * Call an input method `visit_func` on the node\n",
|
||||||
|
"* Can we assume this fits in memory?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -203,21 +207,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Implement depth-first searches (in-order, pre-order, post-order traversals) on a binary tree.\n",
|
"## Problem: Implement depth-first traversals (in-order, pre-order, post-order) on a binary tree.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* [Constraints](#Constraints)\n",
|
"* [Constraints](#Constraints)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
@@ -34,6 +34,10 @@
|
|||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Can we assume we already have a Node class with an insert method?\n",
|
"* Can we assume we already have a Node class with an insert method?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* What should we do with each node when we process it?\n",
|
||||||
|
" * Call an input method `visit_func` on the node\n",
|
||||||
|
"* Can we assume this fits in memory?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -67,6 +71,10 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"Note:\n",
|
||||||
|
"\n",
|
||||||
|
"* This following are all forms of depth-first traversals\n",
|
||||||
|
"\n",
|
||||||
"### In-Order Traversal\n",
|
"### In-Order Traversal\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Recursively call in-order traversal on the left child\n",
|
"* Recursively call in-order traversal on the left child\n",
|
||||||
@@ -77,9 +85,6 @@
|
|||||||
"* Time: O(n)\n",
|
"* Time: O(n)\n",
|
||||||
"* Space: O(m), where m is the recursion depth, or O(1) if using an iterative approach\n",
|
"* Space: O(m), where m is the recursion depth, or O(1) if using an iterative approach\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Note:\n",
|
|
||||||
"* This is a form of a depth-first traversal\n",
|
|
||||||
"\n",
|
|
||||||
"### Pre-Order Traversal\n",
|
"### Pre-Order Traversal\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Visit the current node\n",
|
"* Visit the current node\n",
|
||||||
@@ -90,9 +95,6 @@
|
|||||||
"* Time: O(n)\n",
|
"* Time: O(n)\n",
|
||||||
"* Space: O(m), where m is the recursion depth, or O(1) if using an iterative approach\n",
|
"* Space: O(m), where m is the recursion depth, or O(1) if using an iterative approach\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Note:\n",
|
|
||||||
"* This is a form of a depth-first traversal\n",
|
|
||||||
"\n",
|
|
||||||
"### Post-Order Traversal\n",
|
"### Post-Order Traversal\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Recursively call post-order traversal on the left child\n",
|
"* Recursively call post-order traversal on the left child\n",
|
||||||
@@ -101,10 +103,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(n)\n",
|
"* Time: O(n)\n",
|
||||||
"* Space: O(m), where m is the recursion depth, or O(1) if using an iterative approach\n",
|
"* Space: O(m), where m is the recursion depth, or O(1) if using an iterative approach"
|
||||||
"\n",
|
|
||||||
"Note:\n",
|
|
||||||
"* This is a form of a depth-first traversal"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -279,7 +278,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
Reference in New Issue
Block a user