二叉搜索树的第K大节点

题目:给定一颗二叉搜索树,请找出其中第K大的节点。
利用树的中序遍历,找出节点

package helen.c;

import java.util.Stack;

public class FindTreeNode {
    public class TreeNode{
        int val;
        TreeNode right;
        TreeNode left;
        TreeNode(){}
        TreeNode(int val){
            this.val=val;
        }
    }
    public static TreeNode findKthNode(TreeNode root,int k){
 //  找出第k大的数
 //中序遍历,遍历出来的数据
        if(root==null||k<=0){
            return null;
        }
        Stack<TreeNode> stack=new Stack<>();
       int count=0;
        while (root!=null||!stack.isEmpty()){
            while (root!=null){
                stack.push(root);
                root=root.left;
            }
//         8
//      6     10
//   5   7   9  11
            if(!stack.isEmpty()){
                root=stack.pop();
                if(++count==k){
                    return root;
                }
               root=root.right;
            }
        }
        return root;
    }


    public TreeNode createBinaryTreeNode(int val){
        TreeNode node=new TreeNode();
        node.val=val;
        node.left=null;
        node.right=null;
        return node;
    }
    public void connectTreeNodes(TreeNode parents,TreeNode left,TreeNode right){
        if(parents!=null){
            parents.left=left;
            parents.right=right;
        }
    }
    void test(String testName, TreeNode pRoot, int k, boolean isNull, int expected){
        if(testName != null)
            System.out.printf("%s begins: ", testName);

        TreeNode pTarget = findKthNode(pRoot, k);
        if((isNull && pTarget == null) || (!isNull && pTarget.val == expected))
            System.out.printf("Passed.\n");
        else
            System.out.printf("FAILED.\n");
    }

    //         8
//         6      10
//       5  7   9  11
    void test1(){
        TreeNode pNode8 = createBinaryTreeNode(8);
        TreeNode pNode6 = createBinaryTreeNode(6);
        TreeNode pNode10 = createBinaryTreeNode(10);
        TreeNode pNode5 = createBinaryTreeNode(5);
        TreeNode pNode7 = createBinaryTreeNode(7);
        TreeNode pNode9 = createBinaryTreeNode(9);
        TreeNode pNode11 = createBinaryTreeNode(11);

        connectTreeNodes(pNode8, pNode6, pNode10);
        connectTreeNodes(pNode6, pNode5, pNode7);
        connectTreeNodes(pNode10, pNode9, pNode11);


        //     8
//         6      10
//       5  7    9   11
        test("TestA0", pNode8, 0, true, -1);
        test("TestA1", pNode8, 1, false, 5);
        test("TestA2", pNode8, 2, false, 6);
        test("TestA3", pNode8, 3, false, 7);
        test("TestA4", pNode8, 4, false, 8);
        test("TestA5", pNode8, 5, false, 9);
        test("TestA6", pNode8, 6, false, 10);
        test("TestA7", pNode8, 7, false, 11);
        test("TestA8", pNode8, 8, true, -1);

        System.out.printf("\n\n");
    }

    //            5
//              /
//             4
//            /
//           3
//          /
//         2
//        /
//       1
    void test2(){
        TreeNode pNode5 = createBinaryTreeNode(5);
        TreeNode pNode4 = createBinaryTreeNode(4);
        TreeNode pNode3 = createBinaryTreeNode(3);
        TreeNode pNode2 = createBinaryTreeNode(2);
        TreeNode pNode1 = createBinaryTreeNode(1);

        connectTreeNodes(pNode5, pNode4, null);
        connectTreeNodes(pNode4, pNode3, null);
        connectTreeNodes(pNode3, pNode2, null);
        connectTreeNodes(pNode2, pNode1, null);

        test("TestB0", pNode5, 0, true, -1);
        test("TestB1", pNode5, 1, false, 1);
        test("TestB2", pNode5, 2, false, 2);
        test("TestB3", pNode5, 3, false, 3);
        test("TestB4", pNode5, 4, false, 4);
        test("TestB5", pNode5, 5, false, 5);
        test("TestB6", pNode5, 6, true, -1);

        System.out.printf("\n\n");
    }
    //1
//  \
//   2
//    \
//     3
//      \
//       4
//        \
//         5
    void test3(){
        TreeNode pNode1 = createBinaryTreeNode(1);
        TreeNode pNode2 = createBinaryTreeNode(2);
        TreeNode pNode3 = createBinaryTreeNode(3);
        TreeNode pNode4 = createBinaryTreeNode(4);
        TreeNode pNode5 = createBinaryTreeNode(5);

        connectTreeNodes(pNode1, null, pNode2);
        connectTreeNodes(pNode2, null, pNode3);
        connectTreeNodes(pNode3, null, pNode4);
        connectTreeNodes(pNode4, null, pNode5);

        test("TestC0", pNode1, 0, true, -1);
        test("TestC1", pNode1, 1, false, 1);
        test("TestC2", pNode1, 2, false, 2);
        test("TestC3", pNode1, 3, false, 3);
        test("TestC4", pNode1, 4, false, 4);
        test("TestC5", pNode1, 5, false, 5);
        test("TestC6", pNode1, 6, true, -1);

        System.out.printf("\n\n");
    }

    // There is only one node in a tree
    void test4(){
        TreeNode pNode1 = createBinaryTreeNode(1);

        test("TestD0", pNode1, 0, true, -1);
        test("TestD1", pNode1, 1, false, 1);
        test("TestD2", pNode1, 2, true, -1);
        System.out.printf("\n\n");
    }

    // empty tree
    void test5(){
        test("TestE0", null, 0, true, -1);
        test("TestE1", null, 1, true, -1);
        System.out.printf("\n\n");
    }

    public static void main(String args[]){
        new FindTreeNode().test1();
        new FindTreeNode().test2();
        new FindTreeNode().test3();
        new FindTreeNode().test4();
        new FindTreeNode().test5();
    }
}

中序二叉搜索树的关键在于先将所有的左子树放入栈中,
利用while循环,每出栈一次都要进行右子树的遍历和所有左子树的入栈,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值