<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刷題實戰(zhàn)199:二叉樹的右視圖

          共 5359字,需瀏覽 11分鐘

           ·

          2021-03-02 13:57

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

          今天和大家聊的問題叫做 二叉樹的右視圖,我們先來看題面:
          https://leetcode-cn.com/problems/binary-tree-right-side-view/

          Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

          題意


          給定一棵二叉樹,想象自己站在它的右側(cè),按照從頂部到底部的順序,返回從右側(cè)所能看到的節(jié)點值。

          示例


          解題

          https://www.imooc.com/article/303711

          思路一:廣度優(yōu)化搜索


          當對二叉樹進行層次遍歷時,每一層最右邊的節(jié)點是最后訪問的。題目中要求返回從右側(cè)所能看到的節(jié)點值,正是這里每層最右邊的節(jié)點。那么保留每層最后的訪問節(jié)點,就能得到需要求的答案。
          這里使用隊列存儲。
          具體可參照代碼進行理解。

          class Solution:
              def rightSideView(self, root: TreeNode) -> List[int]:
                  if root == None:
                      return []
                  # 導入 deque 創(chuàng)建隊列
                  from collections import deque

                  queue = deque([root])

                  res = []

                  while queue:
                      size = len(queue)
                      # 這里用 size 記錄二叉樹每層的節(jié)點數(shù),
                      for i in range(size):
                          # 彈出節(jié)點
                          node = queue.popleft()
                          # 先將左節(jié)點入隊
                          if node.left != None:
                              queue.append(node.left)
                          # 再將右節(jié)點入隊
                          if node.right != None:
                              queue.append(node.right)
                          # 隊列先入先出,如果 i 等于 size - 1,
                          # 那么這里就是最右邊的節(jié)點,這個就要得到的結(jié)果,將其放入返回列表中
                          if i == size - 1:
                              res.append(node.val)

                  return res


          思路二:深度優(yōu)化搜索


          同樣的,這道題也能夠使用深度優(yōu)化搜索來解決。
          在搜索的過程中,我們先訪問右子樹,再訪問左子樹。那么每層的第一個節(jié)點就是最右邊節(jié)點。這個時候,只要知道二叉樹的深度,則可以得到最終的答案。
          具體可參照代碼進行理解。

          class Solution:
              def rightSideView(self, root: TreeNode) -> List[int]:
                  res = []

                  def _dfs(node, depth):
                      if node == None:
                          return []

                      # res 的索引表示二叉樹的深度
                      # 若當前的深度的節(jié)點不存在于 res 中,
                      # 表示該層的最右邊節(jié)點還未將其添加到 res 中
                      # 將其加入到節(jié)點中
                      if depth == len(res):
                          res.append(node.val)
                      # 往一下層訪問,先訪問右子樹,在訪問左子樹
                      depth += 1

                      _dfs(node.right, depth)
                      _dfs(node.left, depth)

                  _dfs(root, 0)

                  return res

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

          上期推文:

          LeetCode1-180題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)181:超過經(jīng)理收入的員工
          LeetCode刷題實戰(zhàn)182:查找重復的電子郵箱
          LeetCode刷題實戰(zhàn)183:從不訂購的客戶
          LeetCode刷題實戰(zhàn)184:部門工資最高的員工
          LeetCode刷題實戰(zhàn)185:部門工資前三高的所有員工
          LeetCode刷題實戰(zhàn)186:翻轉(zhuǎn)字符串里的單詞 II
          LeetCode刷題實戰(zhàn)187:重復的DNA序列
          LeetCode刷題實戰(zhàn)188:買賣股票的最佳時機 IV
          LeetCode刷題實戰(zhàn)189:旋轉(zhuǎn)數(shù)組
          LeetCode刷題實戰(zhàn)190:顛倒二進制位
          LeetCode刷題實戰(zhàn)191:位1的個數(shù)
          LeetCode刷題實戰(zhàn)192:統(tǒng)計詞頻
          LeetCode刷題實戰(zhàn)193:有效電話號碼
          LeetCode刷題實戰(zhàn)194:轉(zhuǎn)置文件
          LeetCode刷題實戰(zhàn)195:第十行
          LeetCode刷題實戰(zhàn)196:刪除重復的電子郵箱
          LeetCode刷題實戰(zhàn)197:上升的溫度
          LeetCode刷題實戰(zhàn)198:打家劫舍

          瀏覽 54
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  好好日视频| 国产精品呻吟AV无码 | 国产亚洲精品久久久久久无几年桃 | 国产内射免费观看视频 | 国产日韩欧美视频 |