码迷,mamicode.com
首页 > 其他好文 > 详细

230. Kth Smallest Element in a BST

时间:2018-09-02 11:31:55      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:roo   null   turn   nod   tree   new   class   kth   code   

身体不好

 

 

 

 

 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 }

 

230. Kth Smallest Element in a BST

标签:roo   null   turn   nod   tree   new   class   kth   code   

原文地址:https://www.cnblogs.com/goPanama/p/9573187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!