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 b6d6169

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

File tree

3 files changed

+163
-159
lines changed

3 files changed

+163
-159
lines changed

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

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ A better solution is to eliminate the 2nd for loop and only do one pass.
8383
Algorithm:
8484

8585
- Do one pass through all the prices
86-
- Keep track of the minimum price seen so far.
87-
- calculate `profit = currentPrice - minPriceSoFar`
88-
- Keep track of the maximun profit seen so far.
86+
-- Keep track of the minimum price seen so far.
87+
-- calculate `profit = currentPrice - minPriceSoFar`
88+
-- Keep track of the maximun profit seen so far.
8989
- Return maxProfit.
9090

9191
[source, javascript]
@@ -118,8 +118,8 @@ Another case to take into consideration is that lists might have different lengt
118118

119119
- Have a pointer for each list
120120
- While there's a pointer that is not null, visite them
121-
- Compare each list node's value and take the smaller one.
122-
- Advance the pointer of the taken node to the next one.
121+
-- Compare each list node's value and take the smaller one.
122+
-- Advance the pointer of the taken node to the next one.
123123

124124
*Implementation*:
125125

@@ -163,8 +163,8 @@ A better way to solve this problem is iterating over each character on both list
163163

164164
- Set a pointer to iterate over each node in the lists.
165165
- For each node, have an index (starting at zero) and compare if both lists have the same data.
166-
- When the index reaches the last character on the current node, we move to the next node.
167-
- If we found that a character from one list doesn't match the other, we return `false`.
166+
-- When the index reaches the last character on the current node, we move to the next node.
167+
-- If we found that a character from one list doesn't match the other, we return `false`.
168168

169169
*Implementation*:
170170

@@ -206,8 +206,8 @@ This is a parsing problem, and usually, stacks are good candidates for them.
206206

207207
- Create a mapping for each opening bracket to its closing counterpart.
208208
- Iterate through the string
209-
- When we found an opening bracket, insert the corresponding closing bracket into the stack.
210-
- When we found a closing bracket, pop from the stack and make sure it corresponds to the current character.
209+
-- When we found an opening bracket, insert the corresponding closing bracket into the stack.
210+
-- When we found a closing bracket, pop from the stack and make sure it corresponds to the current character.
211211
- Check the stack is empty. If there's a leftover, it means that something didn't close properly.
212212

213213
*Implementation*:
@@ -242,10 +242,10 @@ Here's an idea: start backward, so we know when there's a warmer temperature bef
242242
*Algorithm*:
243243

244244
- Traverse the daily temperatures backward
245-
- Push each temperature to a stack.
246-
- While the current temperature is larger than the one at the top of the stack, pop it.
247-
- If the stack is empty, then there's no warmer weather ahead, so it's 0.
248-
- If the stack has an element, calculate the index delta.
245+
-- Push each temperature to a stack.
246+
-- While the current temperature is larger than the one at the top of the stack, pop it.
247+
-- If the stack is empty, then there's no warmer weather ahead, so it's 0.
248+
-- If the stack has an element, calculate the index delta.
249249

250250
*Implementation*:
251251

@@ -311,8 +311,8 @@ This game is perfect to practice working with Queues. There are at least two opp
311311
- If the new location has food, remove that eaten food from its queue and place the next food on the map (if any).
312312
- If the new position doesn't have food, remove the tail of the snake since it moved.
313313
- If the snake new position hits itself, game over (return -1). To make this check, we have 2 options:
314-
- Queue: we can visit all the elements on the snake's queue (body) and check if a new position collides. That's `O(n)`
315-
- Set: we can maintain a `set` with all the snake locations, so the check is `O(1)`.
314+
-- Queue: we can visit all the elements on the snake's queue (body) and check if a new position collides. That's `O(n)`
315+
-- Set: we can maintain a `set` with all the snake locations, so the check is `O(1)`.
316316
- Move the snake's head to a new location (enqueue)
317317
- Return the score (snake's length - 1);
318318

@@ -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,15 +395,17 @@ 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

401404
- Enqueue root
402405
- While the queue has an element
403-
- Check the current size of the queue
404-
- Dequeue only `size` times, and for each dequeued node, enqueue their children.
405-
- Check if the node is the last one in its level and add it to the answer.
406+
-- Check the current size of the queue
407+
-- Dequeue only `size` times, and for each dequeued node, enqueue their children.
408+
-- Check if the node is the last one in its level and add it to the answer.
406409

407410
*Implementation*:
408411

‎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 によって変換されたページ (->オリジナル) /