0919. Complete Binary Tree Inserter
https://leetcode.com/problems/complete-binary-tree-inserter
Description
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
Design an algorithm to insert a new node to a complete binary tree keeping it complete after the insertion.
Implement the CBTInserter class:
CBTInserter(TreeNode root)Initializes the data structure with therootof the complete binary tree.int insert(int v)Inserts aTreeNodeinto the tree with valueNode.val == valso that the tree remains complete, and returns the value of the parent of the insertedTreeNode.TreeNode get_root()Returns the root node of the tree.
Example 1:

Constraints:
The number of nodes in the tree will be in the range
[1, 1000].0 <= Node.val <= 5000rootis a complete binary tree.0 <= val <= 5000At most
104calls will be made toinsertandget_root.
ac
Last updated
Was this helpful?