Kth Smallest Element in a BST

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012201343/article/details/49948497
  1. 问题

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

    Note:
    You may assume k is always valid, 1 ≤ k ≤ BST's total elements.

  2. 解答
    class Solution1 {
    public:
         
        int kthSmallest(TreeNode* root, int k) {
            inorderTraversal(root); 
             return res[k-1];
        }
        
      void inorderTraversal(TreeNode* root) 
        {
            if(root)
            {
                
                inorderTraversal(root->left);
                res.push_back(root->val);
                inorderTraversal(root->right);
            }
         //   return res;
        }
        
    private:
         vector<int> res;
    };
    
    class Solution2 {
    public:
         
        int kthSmallest(TreeNode* root, int k) 
        {
             vector<int> res;
             int sum=0;
             vector<TreeNode *> f1;
             
             while(root)
             {
                 f1.push_back(root);
                 root=root->left;
             }
             TreeNode *temp;
             while(!f1.empty() )
             {
                temp=f1.back();
                 
                 
                res.push_back(temp->val);
                sum++;
                if(sum==k) return temp->val;
                f1.pop_back();
                     
                  
                 if(temp->right!=NULL)
                 {
                     TreeNode *f=temp->right;
                     while(f)
                     {
                           f1.push_back(f);
                           f=f->left;
                     }
                 }
               
             }
        }
    };
    
    class Solution {
    public:
         
        int kthSmallest(TreeNode* root, int k) 
        {
            int sum=0;
            stack<TreeNode *> f1;
            // inorder
            while(root || !f1.empty())
            {
                while(root)
                {
                    f1.push(root);
                    root=root->left;
                }
                if(!f1.empty())
                {
                    TreeNode *temp;
                    temp=f1.top();
                    sum++;
                    if(sum==k) return temp->val;
                    f1.pop();
                    
                    if(temp->right!=NULL)
                    {
                        root=temp->right;
                    }
                }
                
                
            }
        }
        
    };


The Kth BST

06-04

Definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree.nnDefinition: A binary search tree(BST) is a binary tree. It may be empty. If it is not empty, it satisfies the following properties:nnEvery elements has a key, and no two elements have the same key, that is, the keys are unique.nThe keys in a nonempty left subtree must be smaller than the key in the root of the subtree.nThe keys in a nonempty right subtree must be larger than the key in the root of the subtree.nThe left and right subtrees are also binary search trees.nIn this problem, we just care about the Preorder Traversal of a BST. Here is the pseudocode for Preorder Traversal:nnvoid preorder(tree_pointer ptr)n/* preorder tree traversal */nn if (ptr) n printf("%d", ptr->data);n preorder(ptr->left_child);n preorder(ptr->right_child);n nnNow, you are given n, the number of nodes in a BST, and the nodes of the BST are consist of the first n lowcase letters. Of course, more than one BST can be constructed except when n is 1. You task is to sort there BST's according to their preorder representations, and gives out the Kth BST.nnFor example, when n is 2, there are two BST's can be constructed, as following:nna bn \ /n b anTheir preorder representations are: ab and ba, so the first one is ab, and the second one is ba.nnInputnnThere are multiple test cases in this problem. The input is terminated by EOF.nnFor each test case, there are two inputs: n and K, representing the number of nodes in the BST, and the index of the BST you need to output.nnNote:nnn is between 1 and 19nK is between 1 and the number of ways to construct the BSTnOutputnnFor each input, you should first output the Kth preorder representation of the BST. Next, for each node (in the order a, b, c, ...), output it first, then output the left sub node (output * if not exist) and the right sub node (output * if not exist), seperated by a single blank space. K will not be greater than the number of representations of BST given n nodes. Output a blank line between two test cases.nnSample Inputnn2 2n4 9nSample Outputnnbana * *nb a *nncbadna * *nb a *nc b dnd * *

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试