Esempio n. 1
0
        private Couple <int> GetTwoMaxValue(BinaryNode <int> root)
        {
            if (root.ChildrenCount() == 0)
            {
                return(new Couple <int>(root.Value, int.MinValue));
            }

            SortedList <int> sortedList = new SortedList <int>(false);

            // Left child
            Couple <int> left = GetTwoMaxValue(root.Left);

            sortedList.Add(left.First);
            sortedList.Add(left.Second);

            // Right child
            if (root.ChildrenCount() == 2)
            {
                Couple <int> right = GetTwoMaxValue(root.Right);
                sortedList.Add(right.First);
                sortedList.Add(right.Second);
            }

            // Root
            sortedList.Add(root.Value);

            // Extract two max values
            List <int> list = sortedList.ToList();

            return(new Couple <int>(list[0], list[1]));
        }
Esempio n. 2
0
        private int CountUniversalSubtree(BinaryNode <int> root)
        {
            int count = 0;

            if (1 == root.ChildrenCount())
            {
                count = CountUniversalSubtree(root.Left);
            }
            else if (2 == root.ChildrenCount())
            {
                count  = CountUniversalSubtree(root.Left);
                count += CountUniversalSubtree(root.Right);
            }

            if (IsUniversalTree(root))
            {
                count++;
            }
            return(count);
        }
Esempio n. 3
0
        private bool IsUniversalTree(BinaryNode <int> root)
        {
            int childrenCount = root.ChildrenCount();

            if (0 == childrenCount)
            {
                return(true);
            }
            if (1 == childrenCount)
            {
                return(IsUniversalTree(root.Left) && root.Value.Equals(root.Left.Value));
            }
            else
            {
                return(IsUniversalTree(root.Left) &&
                       IsUniversalTree(root.Right) &&
                       root.Value.Equals(root.Left.Value) &&
                       root.Value.Equals(root.Right.Value));
            }
        }