230. Kth Smallest Element in a BST(1)
2016-03-14 13:14
288 查看
/**not ac * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int kthSmallest(TreeNode* root, int k) { if(!root) return 0; stack<TreeNode*> s1; s1.push(root);//wrong while(root||!s1.empty()) { while(root) { s1.push(root); root=root->left; } root=s1.top(); s1.pop(); k--; if(!k) return root->val; if(root->right) root=root->right; } } }; /**ac 20ms * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int kthSmallest(TreeNode* root, int k) { if(!root) return 0; stack<TreeNode*> s1; while(root||!s1.empty()) { while(root) { s1.push(root); root=root->left; } root=s1.top(); s1.pop(); k--; if(!k) return root->val; root=root->right; } } };
相关文章推荐
- 226. Invert Binary Tree
- 217. Contains Duplicate
- Android Studio导入第三方jar包
- JSON.parse()和JSON.stringify() 的用法区别
- Android布局适配中一些显示问题
- uva 10891 Game of Sum
- 51Nod--1384全排列
- 206. Reverse Linked List
- 205. Isomorphic Strings
- 189. Rotate Array
- 172. Factorial Trailing Zeroes
- javascript处理日期
- 每个架构师都应该研究下康威定律
- FSMC相关知识
- Django的时区问题
- strace,ltrace linux下跟踪进程调用的命令
- test
- Python IDE
- 171. Excel Sheet Column Number
- 169. Majority Element