JZ62 二叉搜索树的第k小个结点

二叉搜索树的第k个结点

https://www.nowcoder.com/practice/ef068f602dde4d28aab2b210e859150a?tpId=13&&tqId=11215&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

2021年9月12日10:28:54
2021年9月12日10:38:12

加个res保存结果

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    int num = 0;
    TreeNode res = null;

    TreeNode KthNode(TreeNode pRoot, int k) {
        if(pRoot == null) return null;

        KthNode(pRoot.left,k);

        num++;
        if(num == k) res = pRoot;

        KthNode(pRoot.right,k);

        return res;
    }


}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务