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

相关推荐

点赞 评论 收藏
分享
nbdy:字太多了,写简历不是写自传,亮点难点技能点列出来就行,要简明扼要
点赞 评论 收藏
分享
牛客网
牛客企业服务