-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
100 lines (94 loc) · 1.91 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections;
using System.Collections.Generic;
namespace CompleteBinaryTreeInserter
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
{
this.val = val;
this.left = left;
this.right = right;
}
}
class Program
{
static void Main(string[] args)
{
var a = new TreeNode(1);
var b = new TreeNode(2);
var c = new TreeNode(3);
var d = new TreeNode(4);
var e = new TreeNode(5);
var f = new TreeNode(6);
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.left = f;
var inserter = new CBTInserter(a);
inserter.Insert(7);
inserter.Insert(8);
}
}
public class CBTInserter
{
private TreeNode _root;
private List<TreeNode> parents;
private List<TreeNode> children;
public CBTInserter(TreeNode root)
{
_root = root;
var queue = new Queue<TreeNode>();
queue.Enqueue(root);
parents = new List<TreeNode>();
parents.Add(root);
while (queue.Count > 0)
{
int count = queue.Count;
children = new List<TreeNode>();
for (int i = 0; i < count; i++)
{
var cur = queue.Dequeue();
if (cur.left != null)
{
queue.Enqueue(cur.left);
children.Add(cur.left);
}
if (cur.right != null)
{
queue.Enqueue(cur.right);
children.Add(cur.right);
}
}
if(children.Count != count * 2)
break;
parents = children;
}
}
public int Insert(int v)
{
var child = new TreeNode(v);
children.Add(child);
var parent = parents[(children.Count - 1) / 2];
if (children.Count % 2 == 0)
parent.right = child;
else
parent.left = child;
if (children.Count == parents.Count * 2)
{
parents = children;
children = new List<TreeNode>();
}
return parent.val;
}
public TreeNode Get_root()
{
return _root;
}
}
}