二叉树的第k个结点(二叉搜索树的第k个结点)

                                   二叉树的第k个结点

题目描述

给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8)    中,按结点数值大小顺序第三小结点的值为4。

int count = 0;
TreeNode ans;
TreeNode KthNode(TreeNode pRoot, int k){
		helper(pRoot, k);
		return ans;
    }
	
public void helper(TreeNode node, int k){
		if (node==null || count>k) {
			return;
		}
		helper(node.left, k);
		count++;
		if (count == k) {
			ans = node;
		}
		helper(node.right, k);
	}
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    int i = 0;
 
    TreeNode KthNode(TreeNode pRoot, int k) {
        if (pRoot == null) {
            return null;
        }
        //中序遍历次序与大小次序相同
        TreeNode res = KthNode(pRoot.left, k);
        if (res != null) {
            return res;
        }
        i++;
        if (k == i) {
            return pRoot;
        }
        res = KthNode(pRoot.right, k);
        return res;
    }
 
 
}

 方法一:
采用递归的方式先获得中序遍历的结果,然后在从结果中找出第k个节点
代码实现如下

import java.util.ArrayList;

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

    public TreeNode(int val) {
        this.val = val;
    }
}
*/
//二叉搜索树的中序遍历的结果为递增数列
//先求得中序遍历的结果
public class Solution {
    public TreeNode KthNode(TreeNode pRoot, int k)
    {
        
        ArrayList<TreeNode> list = new ArrayList<>();
        inOrderRecur(pRoot,list);
        int len = list.size();
        if(k < 1 || k > len){
            return null;
        } 
        TreeNode result = list.get(k-1);
        return result;
    }
    public void inOrderRecur(TreeNode pRoot,ArrayList<TreeNode> list){
        if(pRoot == null){
            return;
        }
        inOrderRecur(pRoot.left,list);
        list.add(pRoot);
        inOrderRecur(pRoot.right,list);
    }

}

方法二:
采用方法一固然可以,但是时间复杂度过高,只能先获得中序遍历的结果才能找出位于第k个位置的节点,但是若采用非递归的方式,则可以在出栈的时候记录这个节点出栈的位次是否和要求的位置一致,若一致则直接返回该节点即可,非递归的代码实现如下:

import java.util.Stack;
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
//非递归的方式实现中序遍历
public class Solution {
    public TreeNode KthNode(TreeNode pRoot, int k)
    {
        if(pRoot == null || k < 1){
            return null;
        }
        int count = 0;
        TreeNode cur = pRoot;
        Stack<TreeNode> stack = new Stack<>();
        while(cur != null || !stack.isEmpty()){
            while( cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            count++;
            if(count == k){
                return cur;
            }
            cur = cur.right;
        }
        return null;
    }
}

自己写代码

import java.util.ArrayList;
import java.util.List;
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

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

    }

}
*/
public class Solution {
    List<Integer> returnList = new ArrayList<>();
    TreeNode KthNode(TreeNode pRoot, int k)
    {
        if(pRoot == null || k<=0){
            return null;
        }
        helper(pRoot);
        if(returnList.size() < k){
            return null;
        }
        return Find(pRoot,returnList.get(k-1));
    }
    public void helper(TreeNode pRoot){
        if(pRoot == null){
            return;
        }
        helper(pRoot.left);
        returnList.add(pRoot.val);
        helper(pRoot.right);
    }
    public TreeNode Find(TreeNode pRoot,int num){
        if(pRoot == null){
            return null;
        }
        if(pRoot.val == num){
            return pRoot;
        }
        Find(pRoot.left,num);
        Find(pRoot.right,num);
        return null;
    }
}

以上代码有一个问题就是找到了,但是不知道怎么返回

全部评论

相关推荐

今年读完研的我无房无车无对象,月入还没有过万&nbsp;看到他在朋友圈晒房产证,感叹自己白读了这么多年书
梦想是成为七海千秋:那咋了,双9毕业的现在还没存款呢(因为没念完),高中毕业的去直播带货月入几百万也是完全有可能的,退一万步讲,有些人刚出生父母就给买车买房了,上哪说理去,哪怕是同一个起点也会有截然不同的走向,过好自己的生活就完事了。
点赞 评论 收藏
分享
大方的大熊猫准备进厂:1.教育背景:你希望从事什么专业的工作你的主修课就是什么;成绩优秀是你应该做的,没什么可描述的,成绩不优秀也许人家在大学忙着创业呢?(成绩优秀不一定是好事,只能说明多元化的大学你上成了高中,没有真正上明白大学,反而体现了你死板,不爱社交,没有别的突出能力) 2.实践经历:你想表达的意思没有说清楚。你是说你会个性化服务,还是你有实习经历。如果没有带来,经济收益,表彰,更好的发展前景,那你还不如说说提升了自己哪些技能。你说有人给你送锦旗我都能明白你优秀,但是你说你会xxxx,你说这话谁信,证据呢。 3.入伍经历:你描述的就是你的工作职责或者你应该做的,并没有体现出来你把这个事情做好了,而且入伍经历并不能证明你能干好你要应聘的工作,不如只写经历其余所有内容都不写。 4.荣誉技能:重点突出一下,但不要过多描述,这些荣誉的含金量懂得都懂。 重点:你要应聘什么工作(具体岗位,实习生不具体),你的期望薪资
点赞 评论 收藏
分享
心中的变压器:简历需要突出重点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务