public void PrintLevelOrderRecursive(BSTNode root)
        {
            var height = HeightofBST.GetHeight(root);

            for (int i = 0; i <= height; i++)
            {
                PrintGivenLevel(root, i);
            }
        }
Exemple #2
0
        //O(n^2) time complexity
        //O(n) space
        static bool CheckTreeBalancedRecursion(BSTNode root)
        {
            if (root == null)
            {
                return(true);
            }

            var lheight = HeightofBST.GetHeight(root.left);
            var rheight = HeightofBST.GetHeight(root.right);

            return(CheckTreeBalancedRecursion(root.left) && CheckTreeBalancedRecursion(root.right) &&
                   Math.Abs(lheight - rheight) <= 1);
        }
        //static void Main()
        //{
        //    BST bst = new BST();
        //    bst.Insert(3);
        //    bst.Insert(9);
        //    bst.Insert(20);
        //    bst.Insert(null);
        //    bst.Insert(null);
        //    bst.Insert(15);
        //    bst.Insert(7);

        //    var output = GetLevelOrder(bst.root);

        //}

        public static List <List <int> > GetLevelOrder(BSTNode root)
        {
            List <List <int> > lst = new List <List <int> >();

            if (root == null)
            {
                return(lst);
            }

            var h = HeightofBST.GetHeight(root);

            for (int i = 1; i <= h; i++)
            {
                lst.Add(GetLevelOrderRecursive(root, i));
            }

            return(lst);
        }