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

相关推荐

LXXXXd:有点杂,想搞自动化的话没必要把法律的经历写上去
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务