久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    • <bdo id='79Fwg'></bdo><ul id='79Fwg'></ul>

    <legend id='79Fwg'><style id='79Fwg'><dir id='79Fwg'><q id='79Fwg'></q></dir></style></legend>
    1. <small id='79Fwg'></small><noframes id='79Fwg'>

    2. <i id='79Fwg'><tr id='79Fwg'><dt id='79Fwg'><q id='79Fwg'><span id='79Fwg'><b id='79Fwg'><form id='79Fwg'><ins id='79Fwg'></ins><ul id='79Fwg'></ul><sub id='79Fwg'></sub></form><legend id='79Fwg'></legend><bdo id='79Fwg'><pre id='79Fwg'><center id='79Fwg'></center></pre></bdo></b><th id='79Fwg'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='79Fwg'><tfoot id='79Fwg'></tfoot><dl id='79Fwg'><fieldset id='79Fwg'></fieldset></dl></div>
      <tfoot id='79Fwg'></tfoot>

      Java 使用特定格式的級別順序打印二叉樹

      Java Printing a Binary Tree using Level-Order in a Specific Format(Java 使用特定格式的級別順序打印二叉樹)

        <legend id='GuiNH'><style id='GuiNH'><dir id='GuiNH'><q id='GuiNH'></q></dir></style></legend><tfoot id='GuiNH'></tfoot>
        <i id='GuiNH'><tr id='GuiNH'><dt id='GuiNH'><q id='GuiNH'><span id='GuiNH'><b id='GuiNH'><form id='GuiNH'><ins id='GuiNH'></ins><ul id='GuiNH'></ul><sub id='GuiNH'></sub></form><legend id='GuiNH'></legend><bdo id='GuiNH'><pre id='GuiNH'><center id='GuiNH'></center></pre></bdo></b><th id='GuiNH'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='GuiNH'><tfoot id='GuiNH'></tfoot><dl id='GuiNH'><fieldset id='GuiNH'></fieldset></dl></div>
      • <small id='GuiNH'></small><noframes id='GuiNH'>

              <bdo id='GuiNH'></bdo><ul id='GuiNH'></ul>
                <tbody id='GuiNH'></tbody>

              • 本文介紹了Java 使用特定格式的級別順序打印二叉樹的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                好的,我已經閱讀了所有其他相關問題,但找不到對 java 有幫助的問題.我從解讀其他語言的內容中得到了大致的想法;但我還沒有弄清楚.

                Okay, I have read through all the other related questions and cannot find one that helps with java. I get the general idea from deciphering what i can in other languages; but i am yet to figure it out.

                問題:我想對排序進行級別排序(我使用遞歸)并以樹的一般形狀將其打印出來.

                Problem: I would like to level sort (which i have working using recursion) and print it out in the general shape of a tree.

                所以說我有這個:

                    1 
                   / 
                  2   3
                 /   / 
                4   5   6
                

                我的代碼打印出這樣的級別順序:

                My code prints out the level order like this:

                1 2 3 4 5 6
                

                我想這樣打印出來:

                1
                2 3
                4 5 6
                

                現在,在你給我做一個關于我的工作的道德演講之前......我已經完成了我的 AP Comp Sci 項目,當我的老師提到廣度優先搜索的事情時,我對此感到好奇.

                Now before you give me a moral speech about doing my work... I have already finished my AP Comp Sci project and got curious about this when my teacher mentioned the Breadth First Search thing.

                我不知道它是否會有所幫助,但這是我目前的代碼:

                I don't know if it will help, but here is my code so far:

                /**
                  * Calls the levelOrder helper method and prints out in levelOrder.
                  */
                 public void levelOrder()
                 {
                  q = new QueueList();
                  treeHeight = height();
                  levelOrder(myRoot, q, myLevel);
                 }
                
                 /**
                  * Helper method that uses recursion to print out the tree in 
                  * levelOrder
                  */
                 private void levelOrder(TreeNode root, QueueList q, int curLev)
                 {
                  System.out.print(curLev);
                  if(root == null)
                  {
                   return;
                  }
                
                  if(q.isEmpty())
                  {
                   System.out.println(root.getValue());
                  }
                  else
                  {
                   System.out.print((String)q.dequeue()+", ");
                  }
                
                  if(root.getLeft() != null)
                  {
                   q.enqueue(root.getLeft().getValue());
                   System.out.println();
                  }
                  if(root.getRight() != null)
                  {
                   q.enqueue(root.getRight().getValue());
                   System.out.println();
                   curLev++;
                  }
                
                  levelOrder(root.getLeft(),q, curLev);
                  levelOrder(root.getRight(),q, curLev);
                 }
                

                據我所知,我需要使用樹的總高度,并使用一個級別計數器...唯一的問題是我的級別計數器在我的 levelOrder 使用遞歸返回通過樹時不斷計數.

                From what i can figure out, i will need to use the total height of the tree, and use a level counter... Only problem is my level counter keeps counting when my levelOrder uses recursion to go back through the tree.

                對不起,如果這太多了,但一些提示會很好.:)

                Sorry if this is to much, but some tips would be nice. :)

                推薦答案

                這是代碼,這個問題是在一次采訪中問我的...

                Here is the code, this question was asked to me in one of the interviews...

                public void printTree(TreeNode tmpRoot) {
                
                        Queue<TreeNode> currentLevel = new LinkedList<TreeNode>();
                        Queue<TreeNode> nextLevel = new LinkedList<TreeNode>();
                
                        currentLevel.add(tmpRoot);
                
                        while (!currentLevel.isEmpty()) {
                            Iterator<TreeNode> iter = currentLevel.iterator();
                            while (iter.hasNext()) {
                                TreeNode currentNode = iter.next();
                                if (currentNode.left != null) {
                                    nextLevel.add(currentNode.left);
                                }
                                if (currentNode.right != null) {
                                    nextLevel.add(currentNode.right);
                                }
                                System.out.print(currentNode.value + " ");
                            }
                            System.out.println();
                            currentLevel = nextLevel;
                            nextLevel = new LinkedList<TreeNode>();
                
                        }
                
                    }
                

                這篇關于Java 使用特定格式的級別順序打印二叉樹的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
                Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                How to convert Integer to int?(如何將整數轉換為整數?)
                How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
                Inconsistent behavior on java#39;s ==(java的行為不一致==)
                Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)

                    <small id='a6q0c'></small><noframes id='a6q0c'>

                        <tbody id='a6q0c'></tbody>
                      <legend id='a6q0c'><style id='a6q0c'><dir id='a6q0c'><q id='a6q0c'></q></dir></style></legend>
                      • <tfoot id='a6q0c'></tfoot>

                          <bdo id='a6q0c'></bdo><ul id='a6q0c'></ul>

                          <i id='a6q0c'><tr id='a6q0c'><dt id='a6q0c'><q id='a6q0c'><span id='a6q0c'><b id='a6q0c'><form id='a6q0c'><ins id='a6q0c'></ins><ul id='a6q0c'></ul><sub id='a6q0c'></sub></form><legend id='a6q0c'></legend><bdo id='a6q0c'><pre id='a6q0c'><center id='a6q0c'></center></pre></bdo></b><th id='a6q0c'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='a6q0c'><tfoot id='a6q0c'></tfoot><dl id='a6q0c'><fieldset id='a6q0c'></fieldset></dl></div>
                          主站蜘蛛池模板: 观看av| 涩涩视频在线看 | 91久久| 亚洲va国产日韩欧美精品色婷婷 | www.久草.com| 久久久青草婷婷精品综合日韩 | 欧美寡妇偷汉性猛交 | www.日本三级| 国产一区二区三区日韩 | 嫩呦国产一区二区三区av | 色成人免费网站 | 91精品国产综合久久久久久 | 青青草在线视频免费观看 | 日本视频一区二区三区 | 亚洲成人自拍网 | 国产精品国产自产拍高清 | 我要看免费一级毛片 | 久久精品视频12 | 精品一区二区免费视频 | 日日噜| 99热这里有精品 | 最新伦理片 | 国产精品www| 国产精品观看 | 天堂av中文在线 | 九七午夜剧场福利写真 | 一区视频在线 | 欧美精品一区二区三区视频 | 99re视频在线免费观看 | 在线视频日韩精品 | 99在线观看视频 | 日韩一区二区三区在线观看 | 老司机狠狠爱 | 91精品国产91久久久久久丝袜 | 天天色图 | 国产ts人妖系列高潮 | 毛片网站在线观看 | 91美女视频 | 日韩欧美在线一区 | 国产精品乱码一区二区三区 | 精品成人免费视频 |