two bst iterator
bst iterator
- binary search tree iterator ( in order ) follow up:两个BST,然后按顺序iterate。这个follow up建议大家一定不要想当然,要自己动手写一下,考虑齐全corn case,然后跑出来看看,里面有坑。。写173的人都应该已经有现成的iterator了吧 然后便利两个bst 就是用的两个这样的iterator 每次call next 比较大小 加上我原文中提到的trick就没问题了。。有一个比较巧妙的办法,用Integer 而不是int pointer记录每次iterate到的数字,如果发现Integer cur == null,再call next 赋值。
-
Two bst iterator it.next() 4 / \ 3 7 / \ 5 9 3 4 5 7 9 i 1 2 3 4 j 1 2 3 3 4 4 5 7 9 todo : the correct format using the value from one bst iterator and constructor, class, how to run it in eclipse in the constructor Integer i = it1.next(); Integer j = it2.next(); @Override public boolean hasNext(){ return it1.hasNext() || ite2.hasNext(); } @Override public Integer next(){ Integer res; if( j == null || i <= j){ res = i; i = it1.hasNext() ? iter1.next() : null; }else{ res = j; j = it2.hasNext() ? iter2.next() : null; } return res; }
posted on 2018-08-09 17:58 猪猪🐷 阅读(81) 评论(0) 收藏 举报
浙公网安备 33010602011771号