Binary Trees Flashcards

1
Q
  1. Maximum Depth of Binary Tree

Given the root of a binary tree, return its maximum depth.

A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

A
  1. Create base case, which is when it hits a leaf node, where root node is None and make that return 0
  2. Return 1 plus the max of the function call on the left and right node

Basically leaf node will return 1 + max(0,0) and will make its way up the stack/recursion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Given the roots of two binary trees p and q, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

A
  1. Base case where both p and q are null, they are equal, so return True
  2. Case where p or q is null, they are not equal so return False
  3. Case where p.val and q.val is not equal, they are not equal so return False
  4. Last case, is calling on child nodes. So call isSameTree on both the left and right nodes and compare their bool value with “and”
    • if True and True, True
    • If True and False, False
    • If False and False, False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Invert Binary Tree

Given the root of a binary tree, invert the tree, and return its root.

A
  1. Check case where root is none. if so, just return null root
  2. create variables left and right, where they traverse down the left and right trees respectively using recursion
  3. swap the left and right by setting the root.left to right and root.right to left variables.
  4. return the root
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Construct Binary Tree from Preorder and Inorder Traversal
A

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Subtree of Another Tree
A

https://leetcode.com/problems/subtree-of-another-tree/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Binary Tree Level Order Traversal
A

https://leetcode.com/problems/binary-tree-level-order-traversal/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Validate Binary Search Tree
A

https://leetcode.com/problems/validate-binary-search-tree/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Kth Smallest Element in a BST
A

https://leetcode.com/problems/kth-smallest-element-in-a-bst/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Lowest Common Ancestor of a Binary Search Tree
A

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Design Add and Search Words Data Structure
A

https://leetcode.com/problems/design-add-and-search-words-data-structure/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. Implement Trie (Prefix Tree)
A

https://leetcode.com/problems/implement-trie-prefix-tree/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly