230. Kth Smallest Element in a BST

身体不好

 1 class Solution {
 2     public int kthSmallest(TreeNode root, int k) {
 3         Stack<TreeNode> stack = new Stack<TreeNode>();
 4         while(root != null || !stack.empty()) {
 5             while(root != null){
 6                 stack.push(root);
 7                 root = root.left;
 8             }
 9             root = stack.pop();
10             if(--k == 0) return root.val;
11             root = root.right;
12         }
13         return 0;    
14     }
15 }

猜你喜欢

转载自www.cnblogs.com/goPanama/p/9573187.html