首页 > 代码库 > Binary Search Tree Iterator 173

Binary Search Tree Iterator 173

题目描述:

对于一个二叉查找树,设计一个迭代器,每次调用会返回下一个最小值

 


 

题目分析:

没什么好说的二叉树的先序遍历

 

 


 

代码:

 1 /** 2  * Definition for binary tree 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class BSTIterator {11 public:12     BSTIterator(TreeNode *root) {13         14     }15 16     /** @return whether we have a next smallest number */17     bool hasNext() {18         19     }20 21     /** @return the next smallest number */22     int next() {23         24     }25 };26 27 /**28  * Your BSTIterator will be called like this:29  * BSTIterator i = BSTIterator(root);30  * while (i.hasNext()) cout << i.next();31  */

 

Binary Search Tree Iterator 173