iT邦幫忙

2021 iThome 鐵人賽

DAY 17
0
AI & Data

想到甚麼打甚麼系列 第 17

找LeetCode上簡單的題目來撐過30天啦(DAY17)

  • 分享至 

  • xImage
  •  

題號173 標題:Binary Search Tree Iterator 難度:Medium

Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):

BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.
boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise returns false.
int next() Moves the pointer to the right, then returns the number at the pointer.
Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the smallest element in the BST.

You may assume that next() calls will always be valid. That is, there will be at least a next number in the in-order traversal when next() is called.

Example 1:

Input
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
Output
[null, 3, 7, true, 9, true, 15, true, 20, false]

Explanation
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next(); // return 3
bSTIterator.next(); // return 7
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 9
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 15
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 20
bSTIterator.hasNext(); // return False

Constraints:

The number of nodes in the tree is in the range [1, 105].
0 <= Node.val <= 106
At most 105 calls will be made to hasNext, and next.

Follow up:

Could you implement next() and hasNext() to run in average O(1) time and use O(h) memory, where h is the height of the tree?


/**
 * 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 bSTIterator = new TreeNode();
    TreeNode previous  = new TreeNode();
    TreeNode str = new TreeNode();
    public BSTIterator(TreeNode root) {
        bSTIterator =  root; 
        //str = root;
    }
    
    public int next() {
        int result;
        str = bSTIterator;
        while(bSTIterator.left != null){
            previous = bSTIterator;
            bSTIterator = bSTIterator.left;
        }
        if(str == bSTIterator){
            bSTIterator = str.right;
            result = str.val;
            return result;
        }
        if(bSTIterator.right != null){
            previous.left = bSTIterator.right;
        }else{
            previous.left = null;
        }
        result = bSTIterator.val;
        bSTIterator = str;
        return result;
    }
    
    public boolean hasNext() {
        if(bSTIterator!=null){
            return true;
        }
        return false;
    }
}

/**
 * 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();
 */

花比較多的時間在
一樣是link list的了解,果然實作比理論還要學得快?
要做link list的存取,給三個指標,一個指向原本的頭root,一個用來表示現在的位子index,一個來抓temp(看需求,可以存目前最小值,或是前一個點,或是要找的點)

DAY17心得
搞懂以後就寫爆炸快了呢,心情愉悅


上一篇
找LeetCode上簡單的題目來撐過30天啦(DAY16)
下一篇
找LeetCode上簡單的題目來撐過30天啦(DAY18)
系列文
想到甚麼打甚麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言