示例#1
0
        //Logic: we can find the kth smallest by many ways
        //1) we can build sorted array from preorder traversal and find the kth smalles on the sorted array
        //2) we can put items on stack and when stack count reaches k return the element
        //3) use static count inside the method and when k reaches counter return the element

        //public KarthicBTNode<int> FindkthsmallestnodebyInordertraversal(KarthicBTNode<int> root, int k, Stack<KarthicBTNode<int>> mystack)
        //{
        //    //base case
        //    if (root != null)
        //    {
        //        //left
        //        FindkthsmallestnodebyInordertraversal(root.Left, k, mystack);
        //        //current
        //        mystack.Push(root);

        //        if (k == mystack.Count)
        //        {
        //            return root;
        //        }

        //        //right
        //        FindkthsmallestnodebyInordertraversal(root.Right, k, mystack);

        //    }


        //}

        public BTCustomNode <int> FindkthsmallestnodebyInordertraversal(KarthicBTNode <int> root, int k, Stack <KarthicBTNode <int> > mystack)
        {
            //base case
            if (root == null)
            {
                return(null);
            }


            //left

            BTCustomNode <int> ltree = FindkthsmallestnodebyInordertraversal(root.Left, k, mystack);

            if (ltree != null && ltree.IsSmallest)
            {
                return(ltree);
            }


            //current

            BTCustomNode <int> current = new BTCustomNode <int>();

            current.node = root;
            mystack.Push(root);
            if (k == mystack.Count)
            {
                current.IsSmallest = true;
                return(current);
            }
            else
            {
                current.IsSmallest = false;
            }


            //right
            BTCustomNode <int> rtree = FindkthsmallestnodebyInordertraversal(root.Right, k, mystack);

            if (rtree != null && rtree.IsSmallest)
            {
                return(rtree);
            }



            return(current);
        }
示例#2
0
        private BTCustomNode <int> FindKthLargestByTraversal(KarthicBTNode <int> node, int k, Stack <KarthicBTNode <int> > mystack)
        {
            if (node == null)
            {
                BTCustomNode <int> basetree = new BTCustomNode <int>();
                basetree.node = null;
                return(basetree);
            }
            //traverse right child
            BTCustomNode <int> righttree = new BTCustomNode <int>();

            righttree = FindKthLargestByTraversal(node.Right, k, mystack);
            if (righttree.node != null && righttree.IsLargest)
            {
                return(righttree);
            }

            //visit current
            BTCustomNode <int> current = new BTCustomNode <int>();

            mystack.Push(node);
            current.node = node;
            if (mystack.Count == k)
            {
                current.IsLargest = true;
                return(current);
            }
            else
            {
                current.IsLargest = false;
            }


            BTCustomNode <int> lefttree = new BTCustomNode <int>();

            lefttree = FindKthLargestByTraversal(node.Left, k, mystack);
            if (lefttree.node != null && lefttree.IsLargest)
            {
                return(lefttree);
            }

            return(current);
        }