Binary Tree Flashcards
1
Q
- 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:
- Visit Node.
- Traverse Left.
- Traverse Right.
2
Q
- 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:
- Traverse Left.
- Visit Node.
- Traverse Right.
3
Q
- 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:
- Traverse Left.
- Traverse Right.
- Visit Node.