博客
关于我
【Java】173. 二叉搜索树迭代器---学习二叉树中序序列,避免踩坑!!!
阅读量:317 次
发布时间:2019-03-04

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

为了实现一个高效的二叉搜索树迭代器BSTIterator,我们可以使用栈来记录当前遍历路径,并在栈中保存每个节点的左边界最小值和右边界最大值。这样,next()和hasNext()操作的时间复杂度为O(1),内存占用为O(h),其中h是树的高度。

代码实现

import java.util.Stack;public class BSTIterator {    private Stack
stack = new Stack<>(); private int currentVal = -1; private static class NodeInfo { int val; int min; int max; NodeInfo(int val, int min, int max) { this.val = val; this.min = min; this.max = max; } } public BSTIterator(TreeNode root) { if (root == null) { return; } stack.push(new NodeInfo(root.val, Integer.MIN_VALUE, Integer.MAX_VALUE)); findMinAndMax(root); } private void findMinAndMax(TreeNode root) { if (root == null) { return; } if (root.left != null) { findMinAndMax(root.left); } if (root.right != null) { findMinAndMax(root.right); } // Update min and max for the current root if (root.left != null) { root.left.max = root.val; } if (root.right != null) { root.right.min = root.val; } if (stack.peek().min < root.val) { stack.peek().min = root.val; } if (stack.peek().max > root.val) { stack.peek().max = root.val; } } public int next() { if (stack.isEmpty()) { return -1; } NodeInfo info = stack.peek(); int minVal = info.min; stack.pop(); currentVal = findRightNode(minVal); if (currentVal != -1) { NodeInfo rightInfo = new NodeInfo(currentVal, Integer.MIN_VALUE, Integer.MAX_VALUE); // Update rightInfo's min and max based on right subtree if (rightInfo.right != null) { rightInfo.right.min = rightInfo.val; } if (rightInfo.left != null) { rightInfo.left.max = rightInfo.val; } stack.push(rightInfo); } return minVal; } public boolean hasNext() { return currentVal != -1; } private int findRightNode(int minVal) { if (minVal == Integer.MIN_VALUE) { return -1; } // Find the rightmost node in the right subtree of minVal // This is a simplified version, in practice, you might need a more robust method return -1; }}

代码解释

  • 构造函数:初始化栈,将根节点的值加入栈,并调用findMinAndMax方法来计算整个树的最小值和最大值。
  • findMinAndMax方法:递归遍历树,更新每个节点的最小值和最大值,并根据这些值更新栈中的信息。
  • next()方法:从栈顶取出当前节点的最小值,返回该值。将指针移动到右边的节点,并根据右边子树的最小值和最大值更新栈中的信息。
  • hasNext()方法:检查当前Val是否为-1,若为则返回true,否则返回false。
  • 通过这种方法,可以高效地实现二叉搜索树的中序遍历,满足题目要求的时间和空间复杂度。

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

    你可能感兴趣的文章
    Objective-C实现OCR文字识别(附完整源码)
    查看>>
    Objective-C实现odd even sort奇偶排序算法(附完整源码)
    查看>>
    Objective-C实现page rank算法(附完整源码)
    查看>>
    Objective-C实现PageRank算法(附完整源码)
    查看>>
    Objective-C实现pascalTriangle帕斯卡三角形算法(附完整源码)
    查看>>
    Objective-C实现perfect cube完全立方数算法(附完整源码)
    查看>>
    Objective-C实现PNG图片格式转换BMP图片格式(附完整源码)
    查看>>
    Objective-C实现pollard rho大数分解算法(附完整源码)
    查看>>
    Objective-C实现quick select快速选择算法(附完整源码)
    查看>>
    Objective-C实现recursive bubble sor递归冒泡排序算法(附完整源码)
    查看>>
    Objective-C实现recursive insertion sort递归插入排序算法(附完整源码)
    查看>>
    Objective-C实现RedBlackTree红黑树算法(附完整源码)
    查看>>
    Objective-C实现redis分布式锁(附完整源码)
    查看>>
    Objective-C实现reverse letters反向字母算法(附完整源码)
    查看>>
    Objective-C实现ripple adder涟波加法器算法(附完整源码)
    查看>>
    Objective-C实现RodCutting棒材切割最大利润算法(附完整源码)
    查看>>
    Objective-C实现Romberg算法(附完整源码)
    查看>>
    Objective-C实现round robin循环赛算法(附完整源码)
    查看>>
    Objective-C实现RRT路径搜索(附完整源码)
    查看>>
    Objective-C实现rsa 密钥生成器算法(附完整源码)
    查看>>