Exemple #1
0
        public int?LowestCommonAncestor(DSBinarySearchNode <int> root, int v1, int v2)
        {
            if (root == null)
            {
                return(null);
            }
            if (v1 < root.Data && root.Data > v2)
            {
                return(LowestCommonAncestor(root.Left, v1, v2));
            }
            else if (v1 > root.Data && root.Data < v2)
            {
                return(LowestCommonAncestor(root.Right, v1, v2));
            }

            return(root.Data);
        }
        private static bool isValid(DSBinarySearchNode <int> root)
        {
            if (root == null)
            {
                return(true);
            }

            bool isLeftValid  = true;
            bool isRightValid = true;

            if (root.Left != null)
            {
                if (root.Left.Data < root.Data)
                {
                    isLeftValid = isValid(root.Left);
                }
                else
                {
                    return(false);
                }
            }

            if (root.Right != null)
            {
                if (root.Right.Data > root.Data)
                {
                    isRightValid = isValid(root.Right);
                }
                else
                {
                    return(false);
                }
            }

            return(isLeftValid && isRightValid);
        }
        public static bool IsBinarySearchTree(DSBinarySearchNode <int> root)
        {
            /// Implement method to identify if the given tree is binary search tree or not. Return true if yes or else return false.

            return(isValid(root));
        }
Exemple #4
0
 public BinarySearchTreeIsValidTest()
 {
     this.Tree = new DSBinarySearchNode <int>(10);
 }