• Leetcode 刷题日记 剑指 Offer II 055. 二叉搜索树迭代器


    原题链接(https://leetcode.cn/problems/kTOapQ/)

    题目描述

    实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器:

    • BSTIterator(TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点
      root会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。
    • boolean hasNext() 如果向指针右侧遍历存在数字,则返回 true ;否则返回 false 。
    • int next()将指针向右移动,然后返回指针处的数字。

    注意,指针初始化为一个不存在于 BST 中的数字,所以对 next() 的首次调用将返回 BST 中的最小元素。

    可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 的中序遍历中至少存在一个下一个数字。

    示例

    示例

    输入
    inputs = [“BSTIterator”, “next”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”]
    inputs = [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
    输出
    [null, 3, 7, true, 9, true, 15, true, 20, false]
    解释
    BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
    bSTIterator.next(); // 返回 3
    bSTIterator.next(); // 返回 7
    bSTIterator.hasNext(); // 返回 True
    bSTIterator.next(); // 返回 9
    bSTIterator.hasNext(); // 返回 True
    bSTIterator.next(); // 返回 15
    bSTIterator.hasNext(); // 返回 True
    bSTIterator.next(); // 返回 20
    bSTIterator.hasNext(); // 返回 False

    思路

    对于这个树可以使用一个栈去存储先序遍历后得到的值,每次需要的值就是栈顶的值,当栈没有值了并且栈顶之前也没有取出任何值,那么就意味着指针右侧遍历没有数字了。对于遍历,并没有必要一次性全部将其存入栈,可以维护这个栈,每次取值时进行向左子树的迭代,然后去除最终的值,再将节点的指针指向右子树,下次再继续找左子树即可。

    代码

    /**
     * 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 {
    	TreeNode curr;
    	Stack<TreeNode> stack;
    	public BSTIterator(TreeNode root) {
    		curr=root;
    		stack=new Stack<>();
    		
        }
        
        public int next() {
        	while(curr!=null) {
        		stack.add(curr);
        		curr=curr.left;
        	}
        	curr=stack.pop();
        	int ans=curr.val;
        	curr=curr.right;
    		return ans;
    
        }
        
        public boolean hasNext() {
    		return curr!=null||!stack.isEmpty();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    Cy3.5-PEG-Biotin,Cy3.5-聚乙二醇-生物素,Biotin/mal/dope/dbco-PEG-Cy3.5
    【图像边缘检测】基于matlab自适应阈值的八方向和四方向sobel图像边缘检测【含Matlab源码 2058期】
    公交门户分析与统计系统
    SpringBoot - Failed to determine a suitable driver class
    2023年9月11日
    2.6 Windows驱动开发:使用IO与DPC定时器
    《向量数据库指南》——AI原生向量数据库Milvus Cloud 2.3 Enhancement
    宁德时代最好的电池,给他了
    Matlab:格式化文本
    Unity Audio
  • 原文地址:https://blog.csdn.net/qq_45703684/article/details/126008913