leetcode 230. Kth Smallest Element in a BST
Posted hwd9654
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 230. Kth Smallest Element in a BST相关的知识,希望对你有一定的参考价值。
中序遍历,计数器数到k时,输出当前节点值
class Solution private int count; public int kthSmallest(TreeNode root, int k) count = 0; Stack<TreeNode> stack = new Stack<>(); while(root != null) stack.push(root); root = root.left; while(!stack.isEmpty()) TreeNode cur = stack.pop(); count++; if(count == k) return cur.val; cur = cur.right; while(cur != null) stack.push(cur); cur = cur.left; return 0;
以上是关于leetcode 230. Kth Smallest Element in a BST的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode 230. Kth Smallest Element in a BST
Leetcode 230. Kth Smallest Element in a BST
Leetcode 230: Kth Smallest Element in a BST
leetcode 230. Kth Smallest Element in a BST
[Leetcode] Binary search/tree-230. Kth Smallest Element in a BST