230. Kth Smallest Element in a BST

就是tree的非递归Inorder traversal。

https://leetcode.com/submissions/detail/46458334/

 1     public List<Integer> inorderTraversal(TreeNode root) {
 2         List<Integer> res = new ArrayList<Integer>();
 3         if(root == null) {
 4             return res;
 5         }
 6         Stack<TreeNode> stack = new Stack<TreeNode>();
 7         TreeNode cur = root;
 8         while(!stack.isEmpty() || cur != null) {
 9             if(cur != null) {
10                 stack.push(cur);
11                 cur = cur.left;
12             } else {
13                 TreeNode tmp = stack.pop();
14                 res.add(tmp.val);
15                 cur = tmp.right;
16             }
17         }
18         return res;
19     }

 

posted @ 2016-07-28 10:34  warmland  阅读(126)  评论(0编辑  收藏  举报