-03 one day face questions

Question: Given a binary search tree (BST), find a small tree in the K-node.

Ideas: binary search tree: left child node of the tree is less than, greater than the right child node.

class BSTNode {
    int date;
    BSTNode left;
    BSTNode right;
}
public class FinkKthFromBST {
    public BSTNode bst = null;
    public void finkKthFromBST(BSTNode bst, int k){
        if (bst != null) {
            if (bst.left != null) {
                finkKthFromBST(bst.left,k);
            }
            k--;
            if (k == 0) {
                bst = bst;
                return;
            }
            if (bst.right != null) {
                finkKthFromBST(bst.right,k);
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/heyjia/p/11327894.html