<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)173:二叉搜索樹迭代器

          共 3171字,需瀏覽 7分鐘

           ·

          2021-02-04 08:24

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?二叉搜索樹迭代器??,我們先來看題面:
          https://leetcode-cn.com/problems/binary-search-tree-iterator/


          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.


          題意


          實(shí)現(xiàn)一個(gè)二叉搜索樹迭代器。你將使用二叉搜索樹的根節(jié)點(diǎn)初始化迭代器。
          調(diào)用 next() 將返回二叉搜索樹中的下一個(gè)最小的數(shù)。

          樣例


          解題

          https://codingchaozhang.blog.csdn.net/article/details/109497176

          思路一:對(duì)其進(jìn)行遍歷,存儲(chǔ)?對(duì)其進(jìn)行用棧來存儲(chǔ)

          class?TreeNode{
          ????int?val;
          ????TreeNode left;
          ????TreeNode right;
          ????TreeNode(int?x){
          ??????val = x;
          ????}
          ??}
          ??
          ??class?BSTIterator?{
          ??????int?index;
          ??????ArrayList list;

          ??????public?BSTIterator(TreeNode root)?{
          ??????????this.list?= new?ArrayList<>();
          ??????????this.index = -1;
          ??????????this.inorder(root);
          ??????}
          ??????// 先進(jìn)行中序遍歷
          ??????private?void?inorder(TreeNode root){
          ??????????if(root==null){
          ??????????????return;
          ??????????}
          ??????????inorder(root.left);
          ??????????list.add(root.val);
          ??????????inorder(root.right);
          ??????}
          ??????
          ??????/** @return the next smallest number */
          ??????public?int?next()?{
          ??????????return?list.get(++index);
          ??????}
          ??????
          ??????/** @return whether we have a next smallest number */
          ??????public?boolean hasNext()?{
          ??????????return?this.index+1?< this.list.size();
          ??????}
          ??}


          思路二:對(duì)其進(jìn)行用棧來存儲(chǔ)

          class?BSTIterator?{

          ??????// 棧
          ??????Stack stack;
          ??????public?BSTIterator(TreeNode root)?{
          ??????????// 初始化
          ??????????stack?= new?Stack<>();
          ??????????this.pre(root);
          ??????}
          ??????// 先存儲(chǔ)一部分值
          ??????private?void?pre(TreeNode root){
          ??????????while(root!=null){
          ??????????????stack.push(root);
          ??????????????root = root.left;
          ??????????}
          ??????}
          ??????/** @return the next smallest number */
          ??????public?int?next()?{
          ??????????TreeNode temp = this.stack.pop();

          ??????????if(temp.right!=null){
          ??????????????this.pre(temp.right);
          ??????????}

          ??????????return?temp.val;
          ??????}
          ??????
          ??????/** @return whether we have a next smallest number */
          ??????public?boolean hasNext()?{
          ??????????return?this.stack.size()>0;
          ??????}
          ??}



          好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:

          LeetCode1-160題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)161:相隔為1的編輯距離
          LeetCode刷題實(shí)戰(zhàn)162:尋找峰值
          LeetCode刷題實(shí)戰(zhàn)163:缺失的區(qū)間
          LeetCode刷題實(shí)戰(zhàn)164:最大間距
          LeetCode刷題實(shí)戰(zhàn)165:比較版本號(hào)
          LeetCode刷題實(shí)戰(zhàn)166:分?jǐn)?shù)到小數(shù)
          LeetCode刷題實(shí)戰(zhàn)167:兩數(shù)之和 II - 輸入有序數(shù)組
          LeetCode刷題實(shí)戰(zhàn)168:Excel表列名稱
          LeetCode刷題實(shí)戰(zhàn)169:多數(shù)元素
          LeetCode刷題實(shí)戰(zhàn)170:兩數(shù)之和 III - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)
          LeetCode刷題實(shí)戰(zhàn)171:Excel表列序號(hào)
          LeetCode刷題實(shí)戰(zhàn)172:階乘后的零


          瀏覽 58
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  伊人大久热 | 亚洲色屏 | 欧美黄片入口网站 | 一级学生妹毛片性交免费视频 | 求毛片网站 |