<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)314:二叉樹的豎直遍歷

          共 2838字,需瀏覽 6分鐘

           ·

          2021-07-09 02:44

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

          今天和大家聊的問題叫做 二叉樹的豎直遍歷,我們先來看題面:
          https://leetcode-cn.com/problems/binary-tree-vertical-order-traversal/

          Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column).


          If two nodes are in the same row and column, the order should be from left to right.



          給定一個(gè)二叉樹,返回其結(jié)點(diǎn) 垂直方向(從上到下,逐列)遍歷的值。
          如果兩個(gè)結(jié)點(diǎn)在同一行和列,那么順序則為 從左到右。


          示例



          解題

          https://www.cnblogs.com/grandyang/p/5278930.html

          這道題讓我們豎直遍歷二叉樹,并把每一列存入一個(gè)二維數(shù)組,看題目中給的第一個(gè)例子,3和 15 屬于同一列,3在前,第二個(gè)例子中,3,5,2 在同一列,3在前,5和2緊隨其后,那么隱約的可以感覺到好像是一種層序遍歷的前后順序,如何來確定列的順序呢,這里可以把根節(jié)點(diǎn)給個(gè)序號0,然后開始層序遍歷,凡是左子節(jié)點(diǎn)則序號減1,右子節(jié)點(diǎn)序號加1,這樣可以通過序號來把相同列的節(jié)點(diǎn)值放到一起,用一個(gè) TreeMap 來建立序號和其對應(yīng)的節(jié)點(diǎn)值的映射,用 TreeMap 的另一個(gè)好處是其自動排序功能可以讓列從左到右,由于層序遍歷需要用到 queue,此時(shí) queue 里不能只存節(jié)點(diǎn),而是要存序號和節(jié)點(diǎn)組成的 pair 對兒,這樣每次取出就可以操作序號,而且排入隊(duì)中的節(jié)點(diǎn)也賦上其正確的序號,代碼如下:


          class Solution {
          public:
              vector<vector<int>> verticalOrder(TreeNode* root) {
                  vector<vector<int>> res;
                  if (!root) return res;
                  map<int, vector<int>> m;
                  queue<pair<int, TreeNode*>> q;
                  q.push({0, root});
                  while (!q.empty()) {
                      auto a = q.front(); q.pop();
                      m[a.first].push_back(a.second->val);
                      if (a.second->left) q.push({a.first - 1, a.second->left});
                      if (a.second->right) q.push({a.first + 1, a.second->right});
                  }
                  for (auto a : m) {
                      res.push_back(a.second);
                  }
                  return res;
              }
          };


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

          上期推文:

          LeetCode1-280題匯總,希望對你有點(diǎn)幫助!

          LeetCode刷題實(shí)戰(zhàn)301:刪除無效的括號

          LeetCode刷題實(shí)戰(zhàn)302:包含全部黑色像素的最小矩陣
          LeetCode刷題實(shí)戰(zhàn)303:區(qū)域和檢索 - 數(shù)組不可變
          LeetCode刷題實(shí)戰(zhàn)304:二維區(qū)域和檢索 - 矩陣不可變
          LeetCode刷題實(shí)戰(zhàn)305:島嶼數(shù)量II
          LeetCode刷題實(shí)戰(zhàn)306:累加數(shù)
          LeetCode刷題實(shí)戰(zhàn)307:區(qū)域和檢索 - 數(shù)組可修改
          LeetCode刷題實(shí)戰(zhàn)308:二維區(qū)域和檢索 - 可變
          LeetCode刷題實(shí)戰(zhàn)309:最佳買賣股票時(shí)機(jī)含冷凍期
          LeetCode刷題實(shí)戰(zhàn)310:最小高度樹
          LeetCode刷題實(shí)戰(zhàn)311:稀疏矩陣的乘法
          LeetCode刷題實(shí)戰(zhàn)312:戳氣球
          LeetCode刷題實(shí)戰(zhàn)313:超級丑數(shù)

          瀏覽 53
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

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

          手機(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>
                  黄色一级片电影在线观看 | 91三级片在线 | 亚州色图15p | 在线观看内射 | 中文字幕欧美风情 |