示例#1
0
        public IList <int> InorderTraversal(BinaryTree.Node root)
        {
            List <int> result             = new List <int>();
            Stack <BinaryTree.Node> stack = new Stack <BinaryTree.Node>();

            BinaryTree.Node currentNode = root;

            while (currentNode != null || stack.Count != 0)
            {
                while (currentNode != null)
                {
                    stack.Push(currentNode);
                    currentNode = currentNode.LeftNode;
                }

                if (stack.Count != 0)
                {
                    currentNode = stack.Pop();
                    result.Add(currentNode.Value);
                    currentNode = currentNode.RightNode;
                }
            }

            return(result);
        }
示例#2
0
        public int SumOfLeftLeaves(BinaryTree.Node root)
        {
            if (root == null || (root.LeftNode == null && root.RightNode == null))
            {
                return(0);
            }

            int result = 0;
            Queue <BinaryTree.Node> level = new Queue <BinaryTree.Node>();

            BinaryTree.Node currentNode = null;

            level.Enqueue(root);

            while (level.Count != 0)
            {
                currentNode = level.Dequeue();

                if (currentNode.LeftNode != null && currentNode.LeftNode.LeftNode == null && currentNode.LeftNode.RightNode == null)
                {
                    result += currentNode.LeftNode.Value;
                }
                if (currentNode.LeftNode != null)
                {
                    level.Enqueue(currentNode.LeftNode);
                }
                if (currentNode.RightNode != null)
                {
                    level.Enqueue(currentNode.RightNode);
                }
            }

            return(result);
        }
示例#3
0
        public int FindBottomLeftValue(BinaryTree.Node root)
        {
            BinaryTree.Node         mostLeftNode = null, currentNode = null;
            Queue <BinaryTree.Node> level = new Queue <BinaryTree.Node>();
            int currentLevelCount = 0;

            level.Enqueue(root);

            while (level.Count != 0)
            {
                currentLevelCount = level.Count;
                mostLeftNode      = level.Peek();

                do
                {
                    currentNode = level.Dequeue();

                    if (currentNode.LeftNode != null)
                    {
                        level.Enqueue(currentNode.LeftNode);
                    }
                    if (currentNode.RightNode != null)
                    {
                        level.Enqueue(currentNode.RightNode);
                    }
                }while (--currentLevelCount > 0);
            }

            return(mostLeftNode.Value);
        }
示例#4
0
 private int countLess(BinaryTree.Node current, System.IComparable value)
 {
     if (value == null)
     {
         throw new System.ArgumentException();
     }
     if (current == null)
     {
         return(0);
     }
     else if (current.left == null && current.right == null)
     {
         if (current.value.CompareTo(value) > 0)
         {
             return(1);
         }
     }
     if (current.value.CompareTo(value) > 0)
     {
         return(1 + countLess(current.left, value) + countLess(current.right, value));
     }
     else
     {
         return(countLess(current.left, value) + countLess(current.right, value));
     }
 }
        public void Flatten(BinaryTree.Node root)
        {
            if (root == null)
            {
                return;
            }

            BinaryTree.Node tempNode = null,
                            lastNode = null;

            Flatten(root.LeftNode);
            Flatten(root.RightNode);

            if (root.LeftNode != null)
            {
                tempNode       = root.RightNode;
                root.RightNode = root.LeftNode;
                root.LeftNode  = null;
                lastNode       = root.RightNode;

                while (lastNode.RightNode != null)
                {
                    lastNode = lastNode.RightNode;
                }

                lastNode.RightNode = tempNode;
            }
        }
        public string Tree2str(BinaryTree.Node t)
        {
            if (t == null)
            {
                return(string.Empty);
            }
            else if (t.LeftNode == null && t.RightNode == null)
            {
                return(t.Value.ToString());
            }

            StringBuilder result = new StringBuilder();

            result.Append(t.Value.ToString());
            if (t.RightNode != null)
            {
                ExtendResult(t.LeftNode, result);
                ExtendResult(t.RightNode, result);
            }
            else if (t.LeftNode != null)
            {
                ExtendResult(t.LeftNode, result);
            }

            return(result.ToString());
        }
示例#7
0
    private int countLess(BinaryTree.Node current, System.IComparable value)
    {
        if (current == null)
        {
            return(0);
        }
        int count = 0;

        if (current.value.CompareTo(value) > 0)
        {
            count++;
        }
        if (current.right == null && current.left == null)
        {
            count += 0;
        }
        if (current.right != null)
        {
            count += countLess(current.right, value);
        }
        if (current.left != null)
        {
            count += countLess(current.left, value);
        }
        countLess(current.left, value);
        countLess(current.right, value);
        return(count);
    }
示例#8
0
    public virtual int countLessThan(BinaryTree.Node current, System.IComparable value
                                     )
    {
        int count = 0;

        if (value == null)
        {
            throw new System.ArgumentException();
        }
        if (current == null)
        {
            return(0);
        }
        if (current.left == null && current.right == null)
        {
            if (current.value.CompareTo(value) > 0)
            {
                return(1);
            }
            else
            {
                return(0);
            }
        }
        if (current.value.CompareTo(value) > 0)
        {
            count = 1;
        }
        return(count + countLessThan(current.left, value) + countLessThan(current.right,
                                                                          value));
    }
示例#9
0
        public static void EntryPoint()
        {
            BinaryTree.Node root  = new BinaryTree.Node(1);
            BinaryTree.Node node1 = new BinaryTree.Node(2);
            root.LeftNode = node1;

            (new Question156()).UpsideDownBinaryTree(root);
        }
示例#10
0
        public IList <IList <int> > FindLeaves(BinaryTree.Node root)
        {
            IList <IList <int> > levels = new List <IList <int> >();

            Add(root, levels);

            return(levels);
        }
示例#11
0
        public int MaxDepth(BinaryTree.Node root)
        {
            if (root == null)
            {
                return(0);
            }

            return(Math.Max(MaxDepth(root.LeftNode), MaxDepth(root.RightNode)) + 1);
        }
示例#12
0
        public int GetDepth(BinaryTree.Node node)
        {
            if (node == null)
            {
                return(0);
            }

            return(Math.Max(GetDepth(node.LeftNode) + 1, GetDepth(node.RightNode) + 1));
        }
示例#13
0
        public IList <string> BinaryTreePaths(BinaryTree.Node root)
        {
            List <string> result = new List <string>();

            if (root != null)
            {
                ExtendPath(root, string.Empty, result);
            }

            return(result);
        }
示例#14
0
 public bool IsSymmetric(BinaryTree.Node root)
 {
     if (root == null || (root.LeftNode == null && root.RightNode == null))
     {
         return(true);
     }
     else
     {
         return(CheckSymmetric(root.LeftNode, root.RightNode));
     }
 }
示例#15
0
        private BinaryTree.Node ReadNode(BitStream bs, BinaryTree.Node root)
        {
            var n = root;

            while (!n.HasValue)
            {
                n = n.GetNext(bs.ReadBit());
            }

            return(n);
        }
        private static BinaryTree.Node <int> SeedNode(BinaryTree binaryTree)
        {
            BinaryTree.Node <int> root = null;
            var numbers = new[] { 3, 5, 1, 8, 2, 4, 6, 9, 7 };

            foreach (var number in numbers)
            {
                root = binaryTree.Insert(root, number);
            }
            return(root);
        }
示例#17
0
 private int countmethod(BinaryTree.Node node, System.IComparable input)
 {
     if (node == null)
     {
         return(0);
     }
     if (input.CompareTo(node.value) < 0)
     {
         return(1 + countmethod(node.right, input) + countmethod(node.left, input));
     }
     return(0 + countmethod(node.right, input) + countmethod(node.left, input));
 }
示例#18
0
 private int count(BinaryTree.Node current, object value)
 {
     if (current == null)
     {
         return(0);
     }
     if (current.value.CompareTo(value) > 0)
     {
         return(1 + count(current.left, value) + count(current.right, value));
     }
     return(count(current.left, value) + count(current.right, value));
 }
示例#19
0
 private int helper(BinaryTree.Node current, System.IComparable val)
 {
     if (current == null)
     {
         return(0);
     }
     if (current.value.CompareTo(val) > 0)
     {
         return(helper(current.left, val) + helper(current.right, val) + 1);
     }
     return(helper(current.left, val) + helper(current.right, val));
 }
示例#20
0
 private int h(System.IComparable value, BinaryTree.Node n)
 {
     if (n == null)
     {
         return(0);
     }
     if (value.CompareTo(n.value) < 0)
     {
         return(1 + h(value, n.left) + h(value, n.right));
     }
     return(h(value, n.left) + h(value, n.right));
 }
示例#21
0
 private int countLessThan(System.IComparable a, BinaryTree.Node current)
 {
     if (current == null)
     {
         return(0);
     }
     if (current.value.CompareTo(a) <= 0)
     {
         return(countLessThan(a, current.left) + countLessThan(a, current.right));
     }
     return(countLessThan(a, current.left) + countLessThan(a, current.right) + 1);
 }
示例#22
0
 private int countLessThan(System.IComparable value, BinaryTree.Node a)
 {
     if (a == null)
     {
         return(0);
     }
     if ((value).CompareTo(a.value) < 0)
     {
         return(1 + countLessThan(value, a.left) + countLessThan(value, a.right));
     }
     return(0 + countLessThan(value, a.left) + countLessThan(value, a.right));
 }
示例#23
0
 private int countHelp(BinaryTree.Node current, System.IComparable value)
 {
     if (current == null)
     {
         return(0);
     }
     if (current.value.CompareTo(value) > 0)
     {
         return(1 + countHelp(current.left, value) + countHelp(current.right, value));
     }
     return(countHelp(current.left, value) + countHelp(current.right, value));
 }
示例#24
0
 private int less(BinaryTree.Node current, System.IComparable val)
 {
     if (current == null)
     {
         return(0);
     }
     if ((current.value).CompareTo(val) > 0)
     {
         return(1 + less(current.left, val) + less(current.right, val));
     }
     return(less(current.left, val) + less(current.right, val));
 }
示例#25
0
 public virtual int count(BinaryTree.Node current, System.IComparable value)
 {
     if (current == null)
     {
         return(0);
     }
     if (current.value.CompareTo(value) > 0)
     {
         return(count(current.left, value) + count(current.right, value) + 1);
     }
     return(count(current.left, value) + count(current.right, value));
 }
示例#26
0
 public static void EntryPoint()
 {
     BinaryTree.Node Node1 = new BinaryTree.Node(3);
     BinaryTree.Node Node2 = new BinaryTree.Node(9);
     BinaryTree.Node Node3 = new BinaryTree.Node(20);
     Node1.LeftNode  = Node2;
     Node1.RightNode = Node3;
     BinaryTree.Node Node4 = new BinaryTree.Node(15);
     BinaryTree.Node Node5 = new BinaryTree.Node(7);
     Node2.LeftNode  = Node4;
     Node2.RightNode = Node5;
     (new Question102()).LevelOrder(Node1);
 }
示例#27
0
        public bool HasPathSum(BinaryTree.Node root, int sum)
        {
            if (root == null || (root.RightNode == null && root.LeftNode == null && root.Value != sum))
            {
                return(false);
            }
            else if (root.RightNode == null && root.LeftNode == null && root.Value == sum)
            {
                return(true);
            }

            return(HasPathSum(root.RightNode, sum - root.Value) || HasPathSum(root.LeftNode, sum - root.Value));
        }
示例#28
0
 public virtual int countLessThan(System.IComparable value, BinaryTree.Node current
                                  )
 {
     if (current == null)
     {
         return(0);
     }
     if (current.value.CompareTo(value) > 0)
     {
         return(1 + countLessThan(value, current.left) + countLessThan(value, current.right
                                                                       ));
     }
     return(countLessThan(value, current.left) + countLessThan(value, current.right));
 }
示例#29
0
    private int helper(BinaryTree.Node current, System.IComparable value)
    {
        if (current == null)
        {
            return(0);
        }
        int count = 0;

        if (value.CompareTo(current.value) < 0)
        {
            count = 1;
        }
        return(count + helper(current.right, value) + helper(current.left, value));
    }
示例#30
0
    private int countLessThan(BinaryTree.Node c, System.IComparable value)
    {
        if (c == null)
        {
            return(0);
        }
        int count = 0;

        if (c.value.CompareTo(value) > 0)
        {
            count = 1;
        }
        return(count + countLessThan(c.left, value) + countLessThan(c.right, value));
    }