博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法-树-二叉搜索树迭代器
阅读量:3962 次
发布时间:2019-05-24

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

在这里插入图片描述

在这里插入图片描述

哈希表法 先中序遍历搜索树 然后将值放进数组,然后一个一个取出来

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode() {} *     TreeNode(int val) { this.val = val; } *     TreeNode(int val, TreeNode left, TreeNode right) { *         this.val = val; *         this.left = left; *         this.right = right; *     } * } */class BSTIterator {
private List
list; private int index; public BSTIterator(TreeNode root) {
this.list = new ArrayList<>(); this.index = -1; this.inOrder(root); } private void inOrder(TreeNode root) {
if(root == null) {
return; } inOrder(root.left); list.add(root.val); inOrder(root.right); } public int next() {
return this.list.get(++index); } public boolean hasNext() {
return (this.index + 1) < list.size(); }}/** * Your BSTIterator object will be instantiated and called as such: * BSTIterator obj = new BSTIterator(root); * int param_1 = obj.next(); * boolean param_2 = obj.hasNext(); */

转载地址:http://glhzi.baihongyu.com/

你可能感兴趣的文章
P6-c++内存模型和名称空间-02存储连续性、作用域和链接性
查看>>
P9-c++对象和类-02构造函数和析构函数总结
查看>>
P10-c++对象和类-03this指针详细介绍,详细的例子演示
查看>>
bat备份数据库
查看>>
linux数据库导出结果集且比对 && grep -v ---无法过滤的问题
查看>>
shell函数与自带变量
查看>>
linux下shell获取不到PID
查看>>
sort详解
查看>>
linux,shell中if else if的写法,if elif
查看>>
shell中单引号、双引号、反引号的区别
查看>>
shell脚本死循环方法
查看>>
shell中$*和$@的区别
查看>>
log4cxx 的编译安装过程和使用
查看>>
简单邮件系统程序
查看>>
STL里的multimap使用详解
查看>>
STL 库其中的 std::string用法总结
查看>>
模态对话框的销毁过程与非模态对话的几种销毁方法
查看>>
C++实现http下载 && 24点计算编码风格
查看>>
memcached了解使用和常用命令详解
查看>>
GDB调试各功能总结
查看>>