Skip to content

Commit 28320d9

Browse files
committed
up
1 parent 2210291 commit 28320d9

11 files changed

+432
-457
lines changed

Trees/Trees Interview Problems - SOLUTIONS/.ipynb_checkpoints/Trim a Binary Search Tree - SOLUTION-checkpoint.ipynb

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,7 @@
1919
"___\n",
2020
"We should remove all the nodes whose value is not between min and max. \n",
2121
"\n",
22-
"___\n",
23-
"** Feel free to reference the lecture on Binary Search Tree for the node class, but what it more important here is the logic of your function. In which case your function should be in the form:**"
24-
]
25-
},
26-
{
27-
"cell_type": "code",
28-
"execution_count": 1,
29-
"metadata": {
30-
"collapsed": true
31-
},
32-
"outputs": [],
33-
"source": [
34-
"def trimBST(tree,minVal,maxVal):\n",
35-
" \n",
36-
" print tree.left # LeftChild\n",
37-
" print tree.right # Right Child\n",
38-
" print tree.val # Node's value\n",
39-
" \n",
40-
" pass\n",
41-
"\n",
42-
"# Use tree.left , tree.right , and tree.val as your methods to call"
43-
]
44-
},
45-
{
46-
"cell_type": "markdown",
47-
"metadata": {},
48-
"source": [
49-
"** There is no solution cell because the nature of the code should be more logical and a test would essentially give away the answer. Just focus your answer to fill out the logic of the solution using the methods described above **\n",
50-
"\n",
51-
"## Best of luck!"
22+
"___"
5223
]
5324
},
5425
{

Trees/Trees Interview Problems - SOLUTIONS/Trim a Binary Search Tree - SOLUTION.ipynb

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,7 @@
1919
"___\n",
2020
"We should remove all the nodes whose value is not between min and max. \n",
2121
"\n",
22-
"___\n",
23-
"** Feel free to reference the lecture on Binary Search Tree for the node class, but what it more important here is the logic of your function. In which case your function should be in the form:**"
24-
]
25-
},
26-
{
27-
"cell_type": "code",
28-
"execution_count": 1,
29-
"metadata": {
30-
"collapsed": true
31-
},
32-
"outputs": [],
33-
"source": [
34-
"def trimBST(tree,minVal,maxVal):\n",
35-
" \n",
36-
" print tree.left # LeftChild\n",
37-
" print tree.right # Right Child\n",
38-
" print tree.val # Node's value\n",
39-
" \n",
40-
" pass\n",
41-
"\n",
42-
"# Use tree.left , tree.right , and tree.val as your methods to call"
43-
]
44-
},
45-
{
46-
"cell_type": "markdown",
47-
"metadata": {},
48-
"source": [
49-
"** There is no solution cell because the nature of the code should be more logical and a test would essentially give away the answer. Just focus your answer to fill out the logic of the solution using the methods described above **\n",
50-
"\n",
51-
"## Best of luck!"
22+
"___"
5223
]
5324
},
5425
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Binary Search Tree Check \n",
8+
"\n",
9+
"## Problem Statement\n",
10+
"\n",
11+
"Given a binary tree, check whether it’s a binary search tree or not.\n",
12+
"\n",
13+
"** Again, no solution cell, just worry about your code making sense logically. Hint: Think about tree traversals. **\n",
14+
"\n",
15+
"## Solution\n",
16+
"\n",
17+
"Fill out your solution below:"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 1,
23+
"metadata": {
24+
"collapsed": false
25+
},
26+
"outputs": [],
27+
"source": [
28+
"# Code goes Here"
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"This is a classic interview problem, so feel free to just Google search \"Validate BST\" for more information on this problem!"
36+
]
37+
}
38+
],
39+
"metadata": {
40+
"kernelspec": {
41+
"display_name": "Python 2",
42+
"language": "python",
43+
"name": "python2"
44+
},
45+
"language_info": {
46+
"codemirror_mode": {
47+
"name": "ipython",
48+
"version": 2
49+
},
50+
"file_extension": ".py",
51+
"mimetype": "text/x-python",
52+
"name": "python",
53+
"nbconvert_exporter": "python",
54+
"pygments_lexer": "ipython2",
55+
"version": "2.7.11"
56+
}
57+
},
58+
"nbformat": 4,
59+
"nbformat_minor": 0
60+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Tree Level Order Print \n",
8+
"\n",
9+
"Given a binary tree of integers, print it in level order. The output will contain space between the numbers in the same level, and new line between different levels. For example, if the tree is: \n",
10+
"___\n",
11+
"![title](tree_print.png)\n",
12+
"___\n",
13+
"The output should be: \n",
14+
"\n",
15+
" 1 \n",
16+
" 2 3 \n",
17+
" 4 5 6"
18+
]
19+
},
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {},
23+
"source": [
24+
"## Solution\n",
25+
"\n",
26+
"Fill out your solution below:"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 1,
32+
"metadata": {
33+
"collapsed": true
34+
},
35+
"outputs": [],
36+
"source": [
37+
"class Node:\n",
38+
" def __init__(self, val=None):\n",
39+
" self.left = None\n",
40+
" self.right = None\n",
41+
" self.val = val "
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 2,
47+
"metadata": {
48+
"collapsed": true
49+
},
50+
"outputs": [],
51+
"source": [
52+
"def levelOrderPrint(tree):\n",
53+
" #Code here\n",
54+
" pass"
55+
]
56+
}
57+
],
58+
"metadata": {
59+
"kernelspec": {
60+
"display_name": "Python 2",
61+
"language": "python",
62+
"name": "python2"
63+
},
64+
"language_info": {
65+
"codemirror_mode": {
66+
"name": "ipython",
67+
"version": 2
68+
},
69+
"file_extension": ".py",
70+
"mimetype": "text/x-python",
71+
"name": "python",
72+
"nbconvert_exporter": "python",
73+
"pygments_lexer": "ipython2",
74+
"version": "2.7.11"
75+
}
76+
},
77+
"nbformat": 4,
78+
"nbformat_minor": 0
79+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Trim a Binary Search Tree \n",
8+
"\n",
9+
"## Problem Statement\n",
10+
"\n",
11+
"Given the root of a binary search tree and 2 numbers min and max, trim the tree such that all the numbers in the new tree are between min and max (inclusive). The resulting tree should still be a valid binary search tree. So, if we get this tree as input:\n",
12+
"___\n",
13+
"\n",
14+
"![title](bst1.png)\n",
15+
"___\n",
16+
"and we’re given **min value as 5** and **max value as 13**, then the resulting binary search tree should be: \n",
17+
"___\n",
18+
"![title](bst_trim.png)\n",
19+
"___\n",
20+
"We should remove all the nodes whose value is not between min and max. \n",
21+
"\n",
22+
"___\n",
23+
"** Feel free to reference the lecture on Binary Search Tree for the node class, but what it more important here is the logic of your function. In which case your function should be in the form:**"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 1,
29+
"metadata": {
30+
"collapsed": true
31+
},
32+
"outputs": [],
33+
"source": [
34+
"def trimBST(tree,minVal,maxVal):\n",
35+
" \n",
36+
" print tree.left # LeftChild\n",
37+
" print tree.right # Right Child\n",
38+
" print tree.val # Node's value\n",
39+
" \n",
40+
" pass\n",
41+
"\n",
42+
"# Use tree.left , tree.right , and tree.val as your methods to call"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"** There is no solution cell because the nature of the code should be more logical and a test would essentially give away the answer. Just focus your answer to fill out the logic of the solution using the methods described above **\n",
50+
"\n",
51+
"## Best of luck!"
52+
]
53+
}
54+
],
55+
"metadata": {
56+
"kernelspec": {
57+
"display_name": "Python 2",
58+
"language": "python",
59+
"name": "python2"
60+
},
61+
"language_info": {
62+
"codemirror_mode": {
63+
"name": "ipython",
64+
"version": 2
65+
},
66+
"file_extension": ".py",
67+
"mimetype": "text/x-python",
68+
"name": "python",
69+
"nbconvert_exporter": "python",
70+
"pygments_lexer": "ipython2",
71+
"version": "2.7.11"
72+
}
73+
},
74+
"nbformat": 4,
75+
"nbformat_minor": 0
76+
}

0 commit comments

Comments
 (0)