Example #1
0
        public bool IsBst(TreeNode root)
        {
            // clarification question on this
            if (root == null) return true;

            return this.IsBST(int.MinValue, int.MaxValue, root);

        }
Example #2
0
        private  MaxBstResult InternalGetMaxBstInGivenTree(TreeNode root)
        {
            if(root == null)
            {
                return null;
            }

            MaxBstResult lResult = this.InternalGetMaxBstInGivenTree(root.Left);
            MaxBstResult rResult = this.InternalGetMaxBstInGivenTree(root.Right);

            // Need to handle 4 conditions on the pre order traversal..
            if( lResult == null && rResult == null)
            {
                return new MaxBstResult{IsBST=true,TotalNodes=1,LeftVal=root.Data, RightVal=root.Data};
            }
            else if ( lResult != null && rResult != null)
            {
                if(
                    lResult.LeftVal <= root.Data 
                    && rResult.RightVal>= root.Data
                    && lResult.IsBST
                    && rResult.IsBST)
                {
                    return new MaxBstResult { IsBST = true, TotalNodes = lResult.TotalNodes + rResult.TotalNodes + 1, LeftVal = lResult.LeftVal, RightVal = rResult.RightVal };
                }
                else
                {
                    return new MaxBstResult{IsBST=false, TotalNodes= Math.Max(lResult.TotalNodes, rResult.TotalNodes), LeftVal =0, RightVal=0};
                }
            }
            else if (lResult==null)
            {
                if( rResult.IsBST
                    && root.Data <= rResult.LeftVal)
                {
                    return new MaxBstResult{IsBST=true,TotalNodes=rResult.TotalNodes+1, LeftVal=root.Data,RightVal=rResult.RightVal};
                }
                else
                {
                    return new MaxBstResult{IsBST=false,TotalNodes=rResult.TotalNodes, LeftVal=0, RightVal=0};
                }
            }
            else
            {
                if (lResult.IsBST
                    && root.Data >= lResult.RightVal)
                {
                    return new MaxBstResult { IsBST = true, TotalNodes = lResult.TotalNodes + 1, LeftVal = lResult.LeftVal, RightVal = root.Data};
                }
                else
                {
                    return new MaxBstResult { IsBST = false, TotalNodes = lResult.TotalNodes, LeftVal = 0, RightVal = 0 };
                }
            }
        }
Example #3
0
        private TreeNode InternalBstInsert(int val, TreeNode root)
        {
            if(root == null)
            {
                return new TreeNode(val);
            }

            if(val < root.Data)
            {
                root.Left = this.InternalBstInsert(val, root.Left);
            }
            else
            {
                root.Right = this.InternalBstInsert(val, root.Right);
            }

            return root;
        }
Example #4
0
        private bool IsBST(int min, int max, TreeNode root)
        {
            if (root == null) return true;

            // Actual test for BST
            if( root.Data< min || root.Data > max)
            {
                return false;
            }

            if(!this.IsBST(min, root.Data, root.Left))
            {
                return false;
            }

            if(!this.IsBST(root.Data,max, root.Right))
            {
                return false;
            }

            return true;
        }
Example #5
0
        private void InternalPrint(TreeNode r)
        {

        }
Example #6
0
 public Tree(TreeNode root)
 {
     this.root = root;
 }
Example #7
0
        /// <summary>
        /// Find max BXT in a given tree
        /// </summary>
        /// <returns>Sixe of the tree(total nodes)</returns>
        public int GetMaxBstInGivenTree(TreeNode root)
        {

            return this.InternalGetMaxBstInGivenTree(root).TotalNodes;
        }