Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

​

Reverse Level Order Traversal (easy)

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 2 years ago

Reverse Level Order Traversal Testcases seems wrong. My code unchanged from Level Order Traversal works.

Show 1 reply
A

Anonymous

· 3 years ago

instead of:

result.push_back(currentLevel);

use:

result.insert(result.begin(), currentLevel);

It's a simple change that works. it might incur more moves in the container though.

A

Anonymous

· 3 years ago

In the problem function returns vector<vector<int>> In the solution it returns deque<vector<int>> This is a huge difference. Please update!

A

Anonymous

· 3 years ago

This is a little confusing since you expect that what's in the boiler plate here is suppose to be in the solution

Boiler Plate for Question

class Solution: def traverse(self, root): deq = deque() # TODO: Write your code here result = [list(sublist) for sublist in deq] return result

Solution - FOLLOWS SIMILAR FORMAT AS 'LEVEL ORDER TRAVERSAL' QUESTION

def traverse(root): result = deque() if root is None: return result queue = deque() queue.append(root) while queue: levelSize = len(queue) currentLevel = [] for _ in range(levelSize): currentNode = queue.popleft() # add the node to the current level currentLevel.append(currentNode.val) # insert the children of current node in the que
A

Anonymous

· 3 years ago

Instead of LinkedList to hold the level arrays and adding at the beginning of the LinkedList every time, a Stack<List<Integer>> is another option we could try. It worked for me.

A

Anonymous

· 4 years ago

Instead of appending list at the beginning to reverse, why not use Stack instead?

Show 2 replies

Reading Progress

0%


Vote for new content