中序遍历递归写法还得把遍历的节点放入数组明显麻烦,非递归可以直接过程中判断是否到第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>
点赞

相关推荐

牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
自学java狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务