<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)257:二叉樹的所有路徑

          共 2971字,需瀏覽 6分鐘

           ·

          2021-05-10 14:33

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

          今天和大家聊的問題叫做 二叉樹的所有路徑,我們先來看題面:
          https://leetcode-cn.com/problems/binary-tree-paths/

          Given the root of a binary tree, return all root-to-leaf paths in any order.

          A leaf is a node with no children.

          給定一個二叉樹,返回所有從根節(jié)點到葉子節(jié)點的路徑。說明: 葉子節(jié)點是指沒有子節(jié)點的節(jié)點。

          解題



          /**
           * Definition for a binary tree node.
           * public class TreeNode {
           * int val;
           * TreeNode left;
           * TreeNode right;
           * TreeNode(int x) { val = x; }
           * }
           */

          class Solution {
              public  List<String> binaryTreePaths(TreeNode root) {
                  List<String> list = new LinkedList<>();
                  helper(root, "", list);
                  return list;
              }
              
              private void helper(TreeNode root, String s, List<String>list){
                  if (root == null){
                      return;
                  }
                  
                  s = s + root.val;
                  
                  if (root.left == null && root.right == null){
                      list.add(s);
                      return;
                  }
                  
                  if (root.left != null){
                      helper(root.left, s + "->", list);
                  }

                  if (root.right != null){
                      helper(root.right, s + "->", list);
                  }
              }
           
              
           
          }


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

          上期推文:

          LeetCode1-240題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)241:為運算表達式設計優(yōu)先級

          LeetCode刷題實戰(zhàn)242:有效的字母異位詞

          LeetCode刷題實戰(zhàn)243:最短單詞距離

          LeetCode刷題實戰(zhàn)244:最短單詞距離 II

          LeetCode刷題實戰(zhàn)245:最短單詞距離 III

          LeetCode刷題實戰(zhàn)246:中心對稱數(shù)

          LeetCode刷題實戰(zhàn)247:中心對稱數(shù)II

          LeetCode刷題實戰(zhàn)248:中心對稱數(shù)III

          LeetCode刷題實戰(zhàn)249:移位字符串分組

          LeetCode刷題實戰(zhàn)250:統(tǒng)計同值子樹

          LeetCode刷題實戰(zhàn)251:展開二維向量

          LeetCode刷題實戰(zhàn)252:會議室

          LeetCode刷題實戰(zhàn)253:會議室II

          LeetCode刷題實戰(zhàn)254:因子的組合

          LeetCode刷題實戰(zhàn)255:驗證前序遍歷序列二叉搜索樹
          LeetCode刷題實戰(zhàn)256:粉刷房子

          瀏覽 65
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  天天干女人在线视频免费观看 | 欧美老熟妇乱子伦视频 | 国产成人高精内射 | 家庭乱来影音先锋 | 爱爱大全在线 |