Beispiel #1
0
        static void levelOrder(Node root)
        {
            StringBuilder sb = new StringBuilder();
            bfs(root, sb);

            Console.WriteLine(sb.ToString().TrimEnd());
        }
Beispiel #2
0
 static Node insert(Node root, int data)
 {
     if (root == null)
     {
         return new Node(data);
     }
     else {
         Node cur;
         if (data <= root.data)
         {
             cur = insert(root.left, data);
             root.left = cur;
         }
         else {
             cur = insert(root.right, data);
             root.right = cur;
         }
         return root;
     }
 }
Beispiel #3
0
        static void bfs(Node root, StringBuilder sb)
        {
            Queue<Node> queue = new Queue<Node>();
            queue.Enqueue(root);

            while (queue.Count > 0)
            {
                Node n = queue.Dequeue();
                sb.Append(n.data);
                sb.Append(" ");
                if (n.left != null)
                {
                    queue.Enqueue(n.left);
                }
                if (n.right != null)
                {
                    queue.Enqueue(n.right);
                }
            }
        }
Beispiel #4
0
 public Node(int data)
 {
     this.data = data;
     left = right = null;
 }