📣 独立日限时特惠: VIP通行证立减$68
查看: 896| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] Binary Search Tree Iterator 理解

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Binary Search Tree Iterator 有一行代码一直不理解,为什么要声明        
        TreeNode curt = stack.peek();
        TreeNode node = curt;
直接 声明  TreeNode node = stack.peek(); 然后返回node.val不行吗? 谢谢大家

public class BSTIterator {
    private Stack<TreeNode> stack = new Stack<>();

    // @param root: The root of binary tree.
    public BSTIterator(TreeNode root) {
        while (root != null) {
            stack.push(root);
            root = root.left;
        }
    }

    //@return: True if there has next node, or false
    public boolean hasNext() {
        return !stack.isEmpty();
    }

    //@return: return next node
    public TreeNode next() {
        TreeNode curt = stack.peek();
        TreeNode node = curt;

        // move to the next node
        if (node.right == null) {
            node = stack.pop();
            while (!stack.isEmpty() && stack.peek().right == node) {
                node = stack.pop();
            }
        } else {
            node = node.right;
            while (node != null) {
                stack.push(node);
                node = node.left;
            }
        }

        return curt;
    }
}


上一篇:parse string problem
下一篇:Dijstra最短路径算法,priorityqueue是必须的吗?
🔗
gavin92 2018-12-30 09:31:17 | 只看该作者
全局:
node在下面一个if else之后就不一定还是curt了啊。
回复

使用道具 举报

🔗
 楼主| limao123 2018-12-30 11:31:28 | 只看该作者
全局:
gavin92 发表于 2018-12-30 09:31
node在下面一个if else之后就不一定还是curt了啊。

谢谢回复,没太明白,可不可以详细说下,非常感谢
public class BSTIterator {
    private Stack<TreeNode> stack = new Stack<>();

    // @param root: The root of binary tree.
    public BSTIterator(TreeNode root) {
        while (root != null) {
            stack.push(root);
            root = root.left;
        }
    }

    //@return: True if there has next node, or false
    public boolean hasNext() {
        return !stack.isEmpty();
    }

    //@return: return next node
    public TreeNode next() {
        TreeNode node= stack.peek();

        // move to the next node
        if (node.right == null) {
            node = stack.pop();
            while (!stack.isEmpty() && stack.peek().right == node) {
                node = stack.pop();
            }
        } else {
            node = node.right;
            while (node != null) {
                stack.push(node);
                node = node.left;
            }
        }

        return node;
    }
}
回复

使用道具 举报

🔗
gavin92 2018-12-30 12:29:41 来自APP | 只看该作者
全局:
limao123 发表于 2018/12/30 11:31:28


谢谢回复,没太明白,可不可以详细说下,非常感谢
public class BSTIterator {
    private Stack<TreeNode> stack = new Stack...


我建议你先建一个简单的树跑跑看,尝试看看结果是不是对的。
我怀疑你不太明白这个iterator是怎么work的,你可以开个IDE,单步调试一步步看看几个变量都是怎么变的。

评分

参与人数 1大米 +3 收起 理由
limao123 + 3 非常感谢 刚学会加米

查看全部评分

回复

使用道具 举报

🔗
Airtnp 2018-12-30 13:42:17 | 只看该作者
全局:
你把返回的node改掉了啊。。
while (!stack.isEmpty() && stack.peek().right == node) {
                node = stack.pop();
            }
这是在作甚。。

评分

参与人数 1大米 +3 收起 理由
limao123 + 3 非常感谢 刚学会加米

查看全部评分

回复

使用道具 举报

🔗
 楼主| limao123 2018-12-30 14:10:57 | 只看该作者
全局:
Airtnp 发表于 2018-12-30 13:42
你把返回的node改掉了啊。。
while (!stack.isEmpty() && stack.peek().right == node) {
               ...

想明白了 非常感谢
回复

使用道具 举报

🔗
 楼主| limao123 2018-12-30 14:30:09 | 只看该作者
全局:
gavin92 发表于 2018-12-30 12:29
我建议你先建一个简单的树跑跑看,尝试看看结果是不是对的。
我怀疑你不太明白这个iterator是怎么work的 ...

试了一下,我以前理解错了,非常感谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表