Binary Search Tree Iterator java

泪湿孤枕 提交于 2021-02-17 04:58:29

问题


I did the LeetCode question Binary Search Tree Iterator. the following code is what I learned from others. One part I didn't understand which is cur = cur.right. Since I got the smallest value of cur.val. Why do I need to assign cur.right to cur? When I remove cur = cur.right, it said time limit exceeded. Could someone can help me to explain it?

public class BSTIterator {
    Stack<TreeNode> stack;
    TreeNode cur;
    public BSTIterator(TreeNode root) {
        stack = new Stack<>();
        cur = root;
    }

    /** @return the next smallest number */
    public int next() {
        while (cur != null) {
            stack.push(cur);
            cur = cur.left;
        }
        cur = stack.pop();
        int val = cur.val;
        cur = cur.right;  //why needed to assign cur.right to cur? 
        return val;
    }
} 

回答1:


By thinking on the structure of a binary search tree we know that the left node is less than the parent (or middle) node, and that the right node is more than the parent (or middle) node. By setting the current node to be equal to the right node you are iterating through the tree in order from least to largest value. Note that if cur.right() doesn't exist then cur will be set to null and therefore not execute the while loop.




回答2:


I submitted my code, it was successful. https://leetcode.com/problems/binary-search-tree-iterator/

You can find it here. https://github.com/yan-khonski-it/bst/blob/master/bst-core/src/main/java/com/yk/training/bst/iterators/BSTIterator.java

Explanation. You have to use inorder which first visits the left sub-tree, then visit the current node, and then the right sub-tree, so you will iterate through all the elements in the ascending order.

Now, you have stack, which holds all the node that you should return in next call in the correct order.

next will remove the last element from the stack. Now you check the current node, if it has right subtree. If so, you need to iterate though left elements of the right subtree.

   /**
     * Find next node to be returned in {@link #next()}.
     * Push it to stack.
     */
    public void navigateLeftSubtree() {
        stack.push(currentNode);

        while (currentNode.left != null) {
            currentNode = currentNode.left;
            stack.push(currentNode);
        }
    }

In this case, (right sub tree is present for current node), you should put the right child of the current node into the stack. You don't want to put the current into the stack, if you have already visited it.

    public int next() {
        currentNode = stack.pop();
        final int currentValue = currentNode.value;

        if (currentNode.right != null) {
            // Push root of the right subtree into stack.
            currentNode = currentNode.right;
            navigateLeftSubtree();
        }

        return currentValue;
    }


来源:https://stackoverflow.com/questions/50864825/binary-search-tree-iterator-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!