zoukankan      html  css  js  c++  java
  • 63:二叉搜索树的第k个结点

    /**
     * 面试题63:二叉搜索树的第k个结点
     * 给定一颗二叉搜索树,请找出其中的第k大的结点
     * 例如, 5 /  3 7 / / 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
     */
    public class _63_binary_k_num {
        public static void main(String[] args) {
            int[] a={8,6,10,5,7,9,11};
            TreeNode63 treeNode63 = new TreeNode63(8);
            treeNode63.left= new TreeNode63(6);
            treeNode63.right= new TreeNode63(10);
            treeNode63.left.left= new TreeNode63(5);
            treeNode63.left.right= new TreeNode63(7);
            treeNode63.right.left= new TreeNode63(9);
            treeNode63.right.right= new TreeNode63(11);
    
            Solution63 solution63 = new Solution63();
            TreeNode63 treeNode631 = solution63.KthNode(treeNode63, 3);
            System.out.println(treeNode631.val);
        }
    }
    class Solution63 {
        int k;
        TreeNode63 KthNode(TreeNode63 pRoot, int k) {
            if(pRoot==null||k==0){
                return null;
            }
            this.k=k;
            return getKNum(pRoot);
        }
        public TreeNode63 getKNum(TreeNode63 pRoot){
            TreeNode63 result=null;
            if(pRoot!=null){
                if((result=getKNum(pRoot.left))!=null){
                    System.out.println("上"+result.val);
                    return result;
                }
                if((k--)==1){
                    System.out.println("z"+pRoot.val);
                    return pRoot;
                }
                if((result=getKNum(pRoot.right))!=null){
                    System.out.println("x"+result.val);
                    return result;
                }
            }
            return null;
        }
    }
    class TreeNode63 {
        int val = 0;
        TreeNode63 left = null;
        TreeNode63 right = null;
        public TreeNode63(int val) {
            this.val = val;
        }
    }
    
  • 相关阅读:
    HDU 1584 蜘蛛牌(DFS)
    HDU 1800 Flying to the Mars(贪心)
    zsh: command not found: java (xxx)
    Deepin20安装Mysql8
    Deepin全局菜单
    Ubuntu PPA 解读
    Node安装与配置
    Windows安装配置Maven
    idea 安装 Vue 插件没有Vue component选项
    Linux桌面系统创建应用程序快捷方式
  • 原文地址:https://www.cnblogs.com/andy-zhou/p/6554357.html
Copyright © 2011-2022 走看看