using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace BinaryTreeIterator {
[DebuggerDisplay("{Value} L=#{Left} R=#{Right}")]
public Node(string value) {
public Node Left { get; set; }
public Node Right { get; set; }
public string Value { get; }
public static class BinazryTreeSerializer {
public static Node Deserialize(string source) {
var q = new Queue<Node>();
var root = new Node(source[0].ToString());
while(i < source.Length) {
var current = q.Dequeue();
if(i < source.Length && source[i] != '\0') {
var node = new Node(source[i].ToString());
if(i < source.Length && source[i] != '\0') {
var node = new Node(source[i].ToString());
public class BinaryTreeSortedArray : IEnumerable<Node> {
private readonly Node m_root;
public BinaryTreeSortedArray(Node root) {
public IEnumerator<Node> GetEnumerator() {
return new BinaryTreeIterator(m_root);
IEnumerator IEnumerable.GetEnumerator() {
private class BinaryTreeIterator : IEnumerator<Node> {
private readonly Node m_root;
private readonly Stack<Node> parentsStack = new Stack<Node>();
private void GoToBottomLeft(Node node) {
GoToBottomLeft(node.Left);
public BinaryTreeIterator(Node root) {
Current = parentsStack.Pop();
if(Current.Right == null) {
if(parentsStack.Count == 0) {
GoToBottomLeft(Current.Right);
Current = parentsStack.Pop();
public Node Current { get; private set; }
object IEnumerator.Current {