Created
October 19, 2018 02:43
-
-
Save jianminchen/f7b36abfd29b586e4027a241ece260dc to your computer and use it in GitHub Desktop.
Complete binary tree inserter - mock interview interviewee's code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
// To execute C#, please define "static void Main" on a class | |
// named Solution. | |
class Solution | |
{ | |
static void Main(string[] args) | |
{ | |
for (var i = 0; i < 5; i++) | |
{ | |
Console.WriteLine("Hello, World"); | |
} | |
} | |
} | |
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. | |
Write a data structure CBTInserter that is initialized with a complete binary tree and supports the following operations: | |
CBTInserter(TreeNode root) initializes the data structure on a given tree with head node root; | |
CBTInserter.insert(int v) will insert a TreeNode into the tree with value node.val = v so that the tree remains complete, and | |
returns the value of the parent of the inserted TreeNode; | |
CBTInserter.get_root() will return the head node of the tree. | |
Example 1: | |
Input: inputs = ["CBTInserter","insert","get_root"], inputs = [[[1]],[2],[]] | |
Output: [null,1,[1,2]] | |
Example 2: | |
Input: inputs = ["CBTInserter","insert","insert","get_root"], inputs = [[[1,2,3,4,5,6]],[7],[8],[]] | |
Output: [null,3,4,[1,2,3,4,5,6,7,8]] | |
Note: | |
The initial given tree is complete and contains between 1 and 1000 nodes. | |
CBTInserter.insert is called at most 10000 times per test case. | |
Every value of a given or inserted node is between 0 and 5000. | |
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* public int val; | |
* public TreeNode left; | |
* public TreeNode right; | |
* public TreeNode(int x) { val = x; } | |
* } | |
*/ | |
// q root => left 1, right1 , left1.left, right1 | |
public class CBTInserter { | |
TreeNode rootNode; | |
Queue<TreeNode> q = new Queue<TreeNode>(); | |
Queue<TreeNode> lq = new Queue<TreeNode>(); | |
public CBTInserter(TreeNode root) { | |
this.rootNode = root; | |
q.Enqueue(root); | |
while(q.Count()!=0) | |
{ | |
TreeNode node = q.Dequeue(); | |
if(node.left == null) || | |
(node.right == null) | |
lq.Enqueue(node); | |
if(node.left != null) | |
q.Enqueue(node.left); | |
if(node.right != null) | |
q.Enqueue(node.right); | |
} | |
} | |
public int Insert(int v) { | |
// curr = first find the left most leaf node | |
TreeNode temp = lq.Peek(); | |
lq.Enqueue(new TreeNode(v)); | |
if(temp.left==null) | |
temp.left = lq.Last(); | |
else | |
{ | |
temp.right = lq.Last(); | |
} | |
return temp.val; | |
// create a node and deque the leaf node and add the new node as the new leaf node | |
// curr . | |
} | |
public TreeNode Get_root() { | |
return root; | |
} | |
} | |
/** | |
* Your CBTInserter object will be instantiated and called as such: | |
* CBTInserter obj = new CBTInserter(root); | |
* int param_1 = obj.Insert(v); | |
* TreeNode param_2 = obj.Get_root(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment