<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)103:二叉樹的鋸齒形層次遍歷

          共 982字,需瀏覽 2分鐘

           ·

          2020-11-23 21:25

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

          今天和大家聊的問(wèn)題叫做?二叉樹的鋸齒形層次遍歷,我們先來(lái)看題面:

          https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/

          Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

          題意


          給定一個(gè)二叉樹,返回其節(jié)點(diǎn)值的鋸齒形層次遍歷。(即先從左往右,再?gòu)挠彝筮M(jìn)行下一層遍歷,以此類推,層與層之間交替進(jìn)行)。

          樣例

          解題


          本題有很多解法,不過(guò)今天我想要分享一種更簡(jiǎn)單的方法:就是在在層次遍歷的基礎(chǔ)進(jìn)行修改,判斷是否是偶數(shù)層,如果是,就進(jìn)行反轉(zhuǎn) ,代碼如下:


          class?Solution?{
          ????public?List<List> zigzagLevelOrder(TreeNode root) {
          ????????
          ????????List<List> L=new?ArrayList<List>();
          ????????if(root==null){
          ????????????return?L;
          ????????}
          ????????Queue q=new?LinkedList();
          ????????q.offer(root);
          ????????int count=1;
          ????????
          ????????while(!q.isEmpty()){
          ????????????
          ????????????List list=new?ArrayList();
          ????????????int size=q.size();
          ????????????for(int i=0;i????????????????TreeNode temp=q.poll();
          ????????????????if(temp.left!=null){
          ????????????????????q.offer(temp.left);
          ????????????????}
          ????????????????
          ????????????????if(temp.right!=null){
          ????????????????????q.offer(temp.right);
          ????????????????}
          ???????????????
          ????????????????
          ????????????????list.add(temp.val);
          ????????????}
          ????????????if(count%2!=0){
          ?????????????????L.add(list);
          ????????????}else{
          ????????????????Collections.reverse(list);
          ????????????????L.add(list);
          ????????????}
          ????????????count++;
          ???????????
          ????????}
          ????????return?L;
          ????}
          }


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


          上期推文:

          LeetCode1-100題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)101:對(duì)稱二叉樹
          LeetCode刷題實(shí)戰(zhàn)102:二叉樹的層序遍歷

          瀏覽 29
          點(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>
                  爆乳尤物一区二区三区 | 99爱在线播放 | 日韩黄色免费在线 | 亚洲国产手机在线 | 日日夜夜综合精品 |