找LeetCode上简单的题目来撑过30天啦(DAY17)

题号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心得
搞懂以後就写爆炸快了呢,心情愉悦


<<:  DAY 20 Big Data 5Vs – Variety(速度) EMR (2)

>>:  DAY30-JAVA的Set、List、Map介面

day19 Kotlin coroutine flow with liveData in MVVM

恩,标题不知道怎麽下成中文 在之前的范例里,示范了如何用coroutine做一次性的网路请求,并交结...

Day26- Go with Scylla

前言 前一篇我们介绍了如何在 Go 中对 MySQL 做操作,而 MySQL 为关联式资料库,而今天...

Day 17 ( 中级 ) 立体空间 ( 三度空间 )

立体空间 ( 三度空间 ) 教学原文参考:立体空间 ( 三度空间 ) 这篇文章会介绍,如何在 Scr...

DAY05 - API串接常见问题 - CORS - 概念篇 (2)

昨天,我们知道为什麽会看到CORS的错误讯息,也知道从web server发出request透过浏览...

DAY 10 - 可爱小暴龙

大家好~ 我是五岁~ 今天要来画卡通化的可爱小暴龙~ 因为要画成Q版的可爱暴龙,所以要给他圆圆的大头...