中序遍历递归写法还得把遍历的节点放入数组明显麻烦,非递归可以直接过程中判断是否到第k个数了。 import java.util.LinkedList; public class Solution { TreeNode KthNode(TreeNode pRoot, int k){ LinkedList<treenode> nodeStack = new LinkedList<treenode>(); TreeNode current = pRoot; int count = 0; while (current != null || !nodeStack.isEmpty()) { while (current != null) { nodeStack.addFirst(current); current = current.left; } if (!nodeStack.isEmpty()) { current = nodeStack.removeFirst(); count ++; if (count == k) { return current; } current = current.right; } } return null; } }</treenode></treenode>
点赞

相关推荐

03-02 08:18
集美大学 Java
钱嘛数字而已:没有赛事奖项么?另外,项目经历字有点多哈,建议突出一下重点:用的什么技术,解决什么问题,达到什么效果。
大家都开始春招面试了吗
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务