mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-16 06:18:02 +00:00
Polish check tree balance challenge and solution (#80)
Update constraints, test cases, tests, and code.
This commit is contained in:
@@ -36,7 +36,11 @@
|
||||
"\n",
|
||||
"* Is a balanced tree one where the heights of two sub trees of any node doesn't differ by more than 1?\n",
|
||||
" * Yes\n",
|
||||
"* If this is called on a None input, should we return False?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume we already have a Node class with an insert method?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume this fits memory?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
@@ -46,6 +50,8 @@
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* None -> No\n",
|
||||
"* 1 -> Yes\n",
|
||||
"* 5, 3, 8, 1, 4 -> Yes\n",
|
||||
"* 5, 3, 8, 9, 10 -> No"
|
||||
]
|
||||
@@ -120,7 +126,11 @@
|
||||
"class TestCheckBalance(object):\n",
|
||||
"\n",
|
||||
" def test_check_balance(self):\n",
|
||||
" assert_equal(check_balance(None), False)\n",
|
||||
"\n",
|
||||
" node = Node(5)\n",
|
||||
" assert_equal(check_balance(node), True)\n",
|
||||
"\n",
|
||||
" insert(node, 3)\n",
|
||||
" insert(node, 8)\n",
|
||||
" insert(node, 1)\n",
|
||||
@@ -134,6 +144,15 @@
|
||||
" insert(node, 10)\n",
|
||||
" assert_equal(check_balance(node), False)\n",
|
||||
"\n",
|
||||
" node = Node(3)\n",
|
||||
" insert(node, 2)\n",
|
||||
" insert(node, 1)\n",
|
||||
" insert(node, 5)\n",
|
||||
" insert(node, 4)\n",
|
||||
" insert(node, 6)\n",
|
||||
" insert(node, 7)\n",
|
||||
" assert_equal(check_balance(node), False)\n",
|
||||
"\n",
|
||||
" print('Success: test_check_balance')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
@@ -158,21 +177,21 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user