1. Home
  2. Explore
  3. Computer Science
  4. Binary Tree Algorithms

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Binary Tree Algorithms

  • 30Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Tree Basics
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is a tree in computer science?

Tap to flipClick to flipspace

A tree contains nodes (drawn as circles) connected by edges (lines/arrows). Nodes can point to other nodes.

Or swipe the card

30 cards

  1. What is a tree in computer science?

    A tree contains nodes (drawn as circles) connected by edges (lines/arrows). Nodes can point to other nodes.

  2. What terms are used to describe relationships between nodes in a tree?

    Parent: a node that has children. Child: a node directly connected to its parent. Root: a node with no parent. Leaf: a node with no children.

  3. What is the definition of a binary tree?

    A binary tree is a tree where every node has at most two children (0, 1, or 2 children).

  4. What are the three criteria that define a binary tree?

    1. Every node has at most two children.
    2. There is exactly one root (a node with no parent).
    3. There is exactly one path between the root and any node.
  5. Is a tree with exactly one node considered a binary tree?

    Yes, a singleton tree (one node) is a binary tree. It has one root, at most two children (zero), and exactly one path (to itself).

  6. Is an empty tree (zero nodes) considered a binary tree?

    Yes, an empty tree is considered a binary tree. This is a useful edge case for algorithms.

  7. What is the root of a binary tree?

    The root is the node that has no parent (no incoming edges). Typically drawn as the topmost node.

  8. What is a leaf node in a binary tree?

    A leaf node is a node that has no children (no outgoing edges).

  9. In a binary tree, how are the two children typically referred to?

    Left child and right child.

  10. How do you represent a binary tree programmatically (e.g., in JavaScript)?

    Using a Node class with properties: val (the stored value), left (pointer to left child, default null), and right (pointer to right child, default null).

  11. What is a depth-first traversal of a binary tree?

    A traversal that goes deeper in the tree before moving laterally. It visits nodes by going down a branch fully before backtracking.

  12. What data structure is used to implement an iterative depth-first traversal?

    A stack (LIFO - Last In, First Out).

  13. In an iterative depth-first traversal, when do you add a node's value to the result list?

    When the node is popped (removed) from the stack.

  14. How do you ensure the correct left-to-right order in a depth-first traversal using a stack?

    Push the right child first, then the left child. This ensures the left child is on top and visited next.

  15. What are the time and space complexities of a depth-first traversal on a tree with n nodes?

    Time: O(n). Space: O(n).

  16. What is a breadth-first traversal of a binary tree?

    A traversal that visits all nodes level by level, moving across the tree before going deeper.

  17. What data structure is used to implement a breadth-first traversal?

    A queue (FIFO - First In, First Out).

  18. In an iterative breadth-first traversal, when do you add a node's value to the result list?

    When the node is removed (dequeued) from the front of the queue.

  19. Can a breadth-first traversal be easily implemented recursively?

    No, it is not straightforward because recursion uses a stack (LIFO), while breadth-first requires a queue (FIFO).

  20. What is the time complexity of a breadth-first traversal assuming an efficient queue?

    O(n), where n is the number of nodes.

  21. What is the problem 'tree includes'?

    Given a binary tree and a target value, return true if the target value exists in the tree, else false.

  22. How would you implement 'tree includes' using breadth-first search?

    Use a queue initialized with the root. While queue not empty: dequeue a node; if its value equals target, return true; enqueue its existing children. If loop ends, return false.

  23. How would you implement 'tree includes' recursively (depth-first)?

    Base case: if root is null, return false. If root.val equals target, return true. Otherwise, recursively return treeIncludes(root.left, target) || treeIncludes(root.right, target).

  24. What is the problem 'tree sum'?

    Given a binary tree with numeric values, return the total sum of all node values.

  25. How would you compute 'tree sum' recursively?

    Base case: if root is null, return 0. Otherwise, return root.val + treeSum(root.left) + treeSum(root.right).

  26. What is the problem 'tree min value'?

    Given a non-empty binary tree with numeric values, return the smallest value in the tree.

  27. How would you solve 'tree min value' recursively?

    Base case: if root is null, return Infinity. Otherwise, return Math.min(root.val, treeMin(root.left), treeMin(root.right)).

  28. What is the problem 'max root-to-leaf path sum'?

    Given a binary tree with numeric values, return the maximum sum of values along any path from the root to a leaf.

  29. What is the recursive algorithm for 'max root-to-leaf path sum'?

    Base cases: if root is null, return -Infinity; if root is a leaf (no children), return root.val. Otherwise, return root.val + Math.max(maxPathSum(root.left), maxPathSum(root.right)).

  30. Why is -Infinity used as the base case for null nodes in max path sum?

    Because we want to take the maximum; -Infinity is never larger than any actual number, so it won't be selected.

Card 1 of 30
Card 1 of 30

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent