bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 919. Complete Binary Tree Inserter

919. Complete Binary Tree Inserter

Leetcode Solutions

Deque-based Insertion in Complete Binary Tree

  1. Perform a breadth-first search (BFS) on the given tree and add all nodes with 0 or 1 children to the deque.
  2. For each insert operation, take the front node from the deque as the parent node.
  3. Create a new node with the given value and add it as a left or right child of the parent node, depending on which one is available.
  4. Add the new node to the back of the deque.
  5. If the parent node now has two children, remove it from the deque.
  6. Return the value of the parent node.
  7. For the get_root operation, simply return the root of the tree.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...