Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e82c604

Browse files
chore(book/exercises): bst exercises after traversals
1 parent bcb4c28 commit e82c604

File tree

3 files changed

+145
-141
lines changed

3 files changed

+145
-141
lines changed

‎book/D-interview-questions-solutions.asc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,11 @@ As you can see, we opted for using a set to trade speed for memory.
337337
=== Solutions for Binary Tree Questions
338338
(((Interview Questions Solutions, Binary Tree)))
339339

340-
:leveloffset: -1
340+
341+
leveloffset: -1
341342

342343
[#binary-tree-q-diameter-of-binary-tree]
343-
include::content/part03/tree-intro.asc[tag=binary-tree-q-diameter-of-binary-tree]
344+
include::content/part03/binary-search-tree-traversal.asc[tag=binary-tree-q-diameter-of-binary-tree]
344345

345346
We are asked to find the longest path on a binary tree that might or might not pass through the root node.
346347

@@ -384,7 +385,7 @@ We are using `Math.max` to keep track of the longest diameter seen.
384385

385386

386387
[#binary-tree-q-binary-tree-right-side-view]
387-
include::content/part03/tree-intro.asc[tag=binary-tree-q-binary-tree-right-side-view]
388+
include::content/part03/binary-search-tree-traversal.asc[tag=binary-tree-q-binary-tree-right-side-view]
388389

389390
The first thing that might come to mind when you have to visit a tree, level by level, is BFS.
390391
We can visit the tree using a Queue and keep track when a level ends, and the new one starts.
@@ -394,7 +395,9 @@ Since during BFS, we dequeue one node and enqueue their two children (left and r
394395
.There are several ways to solve this problem using BFS. Here are some ideas:
395396
- *1 Queue + Sentinel node*: we can use a special character in the `Queue` like `'*'` or `null` to indicate the level's change. So, we would start something like this `const queue = new Queue([root, '*']);`.
396397
- *2 Queues*: using a "special" character might be seen as hacky, so you can also opt to keep two queues: one for the current level and another for the next level.
397-
- *1 Queue + size tracking*: we track the Queue's `size` before the children are enqueued. That way, we know where the current level ends. We are going to implement this one.
398+
- *1 Queue + size tracking*: we track the Queue's `size` before the children are enqueued. That way, we know where the current level ends.
399+
400+
We are going to implement BFS with "1 Queue + size tracking", since it's arguably the most elegant.
398401

399402
*Algorithm*:
400403

‎book/content/part03/binary-search-tree-traversal.asc

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,141 @@ If we have the following tree:
9292
----
9393

9494
Post-order traverval will return `3, 4, 5, 15, 40, 30, 10`.
95+
96+
97+
==== Practice Questions
98+
(((Interview Questions, Binary Tree)))
99+
100+
101+
// tag::binary-tree-q-diameter-of-binary-tree[]
102+
===== Binary Tree Diameter
103+
104+
*BT-1*) _Find the diameter of a binary tree. A tree's diameter is the longest possible path from two nodes (it doesn't need to include the root). The length of a diameter is calculated by counting the number of edges on the path._
105+
106+
// end::binary-tree-q-diameter-of-binary-tree[]
107+
108+
// _Seen in interviews at: Facebook, Amazon, Google_
109+
110+
// Example 1:
111+
// [graphviz, tree-diameter-example-1, png]
112+
// ....
113+
// graph G {
114+
// 1 -- 2 [color=red]
115+
// 1 -- 3 [color=red]
116+
117+
// 2 -- 4
118+
// 2 -- 5 [color=red]
119+
// }
120+
// ....
121+
122+
// Example 2:
123+
// [graphviz, tree-diameter-example-2, png]
124+
// ....
125+
// graph G {
126+
// 1
127+
// 2
128+
// 3
129+
// 4
130+
// 5
131+
// 6
132+
// "null" [color=white, fontcolor = white]
133+
// "null." [color=white, fontcolor = white]
134+
// 7
135+
// 8
136+
// "null.." [color=white, fontcolor = white]
137+
// "null..." [color=white, fontcolor = white]
138+
// 9
139+
140+
// 1 -- 2
141+
// 1 -- 3
142+
143+
// 3 -- 4 [color=red]
144+
// 3 -- 5 [color=red]
145+
146+
// 4 -- 6 [color=red]
147+
// 4 -- "null." [color=white]
148+
149+
// 5 -- "null" [color=white]
150+
// 5 -- 7 [color=red]
151+
152+
// 6 -- 8 [color=red]
153+
// 6 -- "null.." [color=white]
154+
155+
// 7 -- "null..." [color=white]
156+
// 7 -- 9 [color=red]
157+
// }
158+
// ....
159+
160+
Examples:
161+
162+
[source, javascript]
163+
----
164+
/* 1
165+
/ \
166+
2 3
167+
/ \
168+
4 5 */
169+
diameterOfBinaryTree(toBinaryTree([1,2,3,4,5])); // 3
170+
// For len 3, the path could be 3-1-2-5 or 3-1-2-4
171+
172+
/* 1
173+
/ \
174+
2 3
175+
/ \
176+
4 5
177+
/ \
178+
6 7
179+
/ \
180+
8 9 */
181+
const array = [1,2,3,null,null,4,5,6,null,null,7,8,null,null,9];
182+
const tree = BinaryTreeNode.from(array);
183+
diameterOfBinaryTree(tree); // 6 (path: 8-6-4-3-5-7-9)
184+
----
185+
186+
Starter code:
187+
188+
[source, javascript]
189+
----
190+
include::../../interview-questions/diameter-of-binary-tree.js[tags=description;placeholder]
191+
----
192+
193+
194+
_Solution: <<binary-tree-q-diameter-of-binary-tree>>_
195+
196+
197+
198+
199+
// tag::binary-tree-q-binary-tree-right-side-view[]
200+
===== Binary Tree from right side view
201+
202+
*BT-2*) _Imagine that you are viewing the tree from the right side. What nodes would you see?_
203+
204+
// end::binary-tree-q-binary-tree-right-side-view[]
205+
206+
// _Seen in interviews at: Facebook, Amazon, ByteDance (TikTok)._
207+
208+
Examples:
209+
210+
[source, javascript]
211+
----
212+
/*
213+
1 <- 1 (only node on level)
214+
/ \
215+
2 3 <- 3 (3 is the rightmost)
216+
\
217+
4 <- 4 (only node on level) */
218+
rightSideView(BinaryTreeNode.from([1, 2, 3, null, 4])); // [1, 3, 4]
219+
220+
rightSideView(BinaryTreeNode.from([])); // []
221+
rightSideView(BinaryTreeNode.from([1, 2, 3, null, 5, null, 4, 6])); // [1, 3, 4, 6]
222+
----
223+
224+
Starter code:
225+
226+
[source, javascript]
227+
----
228+
include::../../interview-questions/binary-tree-right-side-view.js[tags=description;placeholder]
229+
----
230+
231+
_Solution: <<binary-tree-q-binary-tree-right-side-view>>_
232+

‎book/content/part03/tree-intro.asc

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -109,140 +109,3 @@ image::image35.png[image,width=258,height=169]
109109
Heap is better at finding max or min values in constant time *O(1)*, while a balanced BST is good a finding any element in *O(log n)*. Heaps are often used to implement priority queues while BST is used when you need every value sorted.
110110
****
111111
indexterm:[Runtime, Logarithmic]
112-
113-
114-
==== Practice Questions
115-
(((Interview Questions, Binary Tree)))
116-
117-
118-
// tag::binary-tree-q-diameter-of-binary-tree[]
119-
===== Binary Tree Diameter
120-
121-
*BT-1*) _Find the diameter of a binary tree. A tree's diameter is the longest possible path from two nodes (it doesn't need to include the root). The length of a diameter is calculated by counting the number of edges on the path._
122-
123-
// end::binary-tree-q-diameter-of-binary-tree[]
124-
125-
// _Seen in interviews at: Facebook, Amazon, Google_
126-
127-
// Example 1:
128-
// [graphviz, tree-diameter-example-1, png]
129-
// ....
130-
// graph G {
131-
// 1 -- 2 [color=red]
132-
// 1 -- 3 [color=red]
133-
134-
// 2 -- 4
135-
// 2 -- 5 [color=red]
136-
// }
137-
// ....
138-
139-
// Example 2:
140-
// [graphviz, tree-diameter-example-2, png]
141-
// ....
142-
// graph G {
143-
// 1
144-
// 2
145-
// 3
146-
// 4
147-
// 5
148-
// 6
149-
// "null" [color=white, fontcolor = white]
150-
// "null." [color=white, fontcolor = white]
151-
// 7
152-
// 8
153-
// "null.." [color=white, fontcolor = white]
154-
// "null..." [color=white, fontcolor = white]
155-
// 9
156-
157-
// 1 -- 2
158-
// 1 -- 3
159-
160-
// 3 -- 4 [color=red]
161-
// 3 -- 5 [color=red]
162-
163-
// 4 -- 6 [color=red]
164-
// 4 -- "null." [color=white]
165-
166-
// 5 -- "null" [color=white]
167-
// 5 -- 7 [color=red]
168-
169-
// 6 -- 8 [color=red]
170-
// 6 -- "null.." [color=white]
171-
172-
// 7 -- "null..." [color=white]
173-
// 7 -- 9 [color=red]
174-
// }
175-
// ....
176-
177-
Examples:
178-
179-
[source, javascript]
180-
----
181-
/* 1
182-
/ \
183-
2 3
184-
/ \
185-
4 5 */
186-
diameterOfBinaryTree(toBinaryTree([1,2,3,4,5])); // 3
187-
// For len 3, the path could be 3-1-2-5 or 3-1-2-4
188-
189-
/* 1
190-
/ \
191-
2 3
192-
/ \
193-
4 5
194-
/ \
195-
6 7
196-
/ \
197-
8 9 */
198-
const array = [1,2,3,null,null,4,5,6,null,null,7,8,null,null,9];
199-
const tree = BinaryTreeNode.from(array);
200-
diameterOfBinaryTree(tree); // 6 (path: 8-6-4-3-5-7-9)
201-
----
202-
203-
Starter code:
204-
205-
[source, javascript]
206-
----
207-
include::../../interview-questions/diameter-of-binary-tree.js[tags=description;placeholder]
208-
----
209-
210-
211-
_Solution: <<binary-tree-q-diameter-of-binary-tree>>_
212-
213-
214-
215-
216-
// tag::binary-tree-q-binary-tree-right-side-view[]
217-
===== Binary Tree from right side view
218-
219-
*BT-2*) _Imagine that you are viewing the tree from the right side. What nodes would you see?_
220-
221-
// end::binary-tree-q-binary-tree-right-side-view[]
222-
223-
// _Seen in interviews at: Facebook, Amazon, ByteDance (TikTok)._
224-
225-
Examples:
226-
227-
[source, javascript]
228-
----
229-
/*
230-
1 <- 1 (only node on level)
231-
/ \
232-
2 3 <- 3 (3 is the rightmost)
233-
\
234-
4 <- 4 (only node on level) */
235-
rightSideView(BinaryTreeNode.from([1, 2, 3, null, 4])); // [1, 3, 4]
236-
237-
rightSideView(BinaryTreeNode.from([])); // []
238-
rightSideView(BinaryTreeNode.from([1, 2, 3, null, 5, null, 4, 6])); // [1, 3, 4, 6]
239-
----
240-
241-
Starter code:
242-
243-
[source, javascript]
244-
----
245-
include::../../interview-questions/binary-tree-right-side-view.js[tags=description;placeholder]
246-
----
247-
248-
_Solution: <<binary-tree-q-binary-tree-right-side-view>>_

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /