Flashcards
Trees LC
Cards in this deck (4)
- 1[LC102] Binary Tree Level Order TraversalAlgo:
1 ) Form a queue and add the root, go till queue is not empty
2 ) Get size of the queue, this will be the row, Add left and right if they exist.
3 ) Finally pop the head of the queue add to the result row.
4 ) Finally add the row to the list. - 2[LC94] Binary Tree InOrder Traversal1 ) Have a stack (Common for all DFS).
2 ) First push all till you hit the left corner.
3 ) Then add left corner to the result.
4 ) Check if right corner has a left, if yes continue to find new left corner.
5 ) Else pop stack and continue. - 3[LC144] Binary Tree PreOrder Traversal1 ) Simpler than the InOrder.
2 ) First add top of stack.
3 ) Add right and left to the stack. (*In that order.*)
4 ) Repeat till stack is empty. - 4[LC145] Binary Tree PostOrder Traversal