Binary Tree Flashcards

1
Q
  1. Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes’ values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,2,3]

TC - O(n)
SC - O(n)

A

Pseudo Code:

  1. Visit Node.
  2. Traverse Left.
  3. Traverse Right.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes’ values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,3,2]

TC - O(n)
SC - O(n)

A

Pseudo Code:

  1. Traverse Left.
  2. Visit Node.
  3. Traverse Right.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes’ values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [3,2,1]

TC - O(n)
SC - O(n)

A

Pseudo Code:

  1. Traverse Left.
  2. Traverse Right.
  3. Visit Node.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly