博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Binary Search Tree Iterator
阅读量:4691 次
发布时间:2019-06-09

本文共 3145 字,大约阅读时间需要 10 分钟。

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.

Calling next() will return the next smallest number in the BST.

Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.

Credits:

Special thanks to  for adding this problem and creating all test cases.

Approach #1: C++.

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class BSTIterator {public:    BSTIterator(TreeNode *root) {        helper(root);    }    /** @return whether we have a next smallest number */    bool hasNext() {        if (minNums.empty()) return false;        else return true;    }    /** @return the next smallest number */    int next() {        TreeNode* cur = minNums.top();        minNums.pop();        helper(cur->right);        return cur->val;    }private:    stack
minNums; void helper(TreeNode* root) { while (root != nullptr) { minNums.push(root); root = root->left; } }};/** * Your BSTIterator will be called like this: * BSTIterator i = BSTIterator(root); * while (i.hasNext()) cout << i.next(); */

  

Approach #2: Java.

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class BSTIterator {    public BSTIterator(TreeNode root) {        helper(root);    }    /** @return whether we have a next smallest number */    public boolean hasNext() {        if (stack.isEmpty()) return false;        return true;    }    /** @return the next smallest number */    public int next() {        TreeNode cur = stack.pop();        helper(cur.right);        return cur.val;    }        private Stack
stack = new Stack
(); private void helper(TreeNode root) { while (root != null) { stack.push(root); root = root.left; } } }/** * Your BSTIterator will be called like this: * BSTIterator i = new BSTIterator(root); * while (i.hasNext()) v[f()] = i.next(); */

  

Approach #3: Python.

# Definition for a  binary tree node# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass BSTIterator(object):    def __init__(self, root):        """        :type root: TreeNode        """        self.stack = list()        self.helper(root)            def hasNext(self):        """        :rtype: bool        """        return self.stack            def next(self):        """        :rtype: int        """        cur = self.stack.pop()        self.helper(cur.right)        return cur.val            def helper(self, root):        while root is not None:            self.stack.append(root)            root = root.left        # Your BSTIterator will be called like this:# i, v = BSTIterator(root), []# while i.hasNext(): v.append(i.next())

  

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/10011307.html

你可能感兴趣的文章
SpringMVC学习笔记之---RESTful风格
查看>>
Mybatis学习笔记之---动态sql中标签的使用
查看>>
Mybatis学习笔记之---CRUD(增删改查)
查看>>
Mybatis学习笔记之---多表查询(2)
查看>>
Mybatis学习笔记之---多表查询(1)
查看>>
SSM整合之---简单选课系统
查看>>
SSM整合之---环境搭建
查看>>
创建.net framework webapi出现“Web 服务器被配置为不列出此目录的内容。”错误
查看>>
c#中关于@的作用
查看>>
Excel转换成xml文件
查看>>
关于Java链接c#的webapi的注意事项
查看>>
关于vs无法创建虚拟目录的问题
查看>>
ad域的那些事儿
查看>>
如何将自己写的程序加入到计算机服务里
查看>>
C# 二维码 ThoughtWorks.QRCode.dll
查看>>
(转)C#使用itextsharp生成PDF文件
查看>>
EventBus
查看>>
生成简单的二维码
查看>>
ok框架简单应用
查看>>
简单的加减器
查看>>