1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實(shí)戰(zhàn)257:二叉樹的所有路徑

        共 2971字,需瀏覽 6分鐘

         ·

        2021-05-10 14:33

        算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(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.

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

        解題



        /**
         * 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);
                }
            }
         
            
         
        }


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

        上期推文:

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

        LeetCode刷題實(shí)戰(zhàn)241:為運(yùn)算表達(dá)式設(shè)計(jì)優(yōu)先級(jí)

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

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

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

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

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

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

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

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

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

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

        LeetCode刷題實(shí)戰(zhàn)252:會(huì)議室

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

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

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

        瀏覽 65
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

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

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            亚洲成人黄色网 | 成人午夜免费福利视频 | 成人喷水 亚洲一区无码 | 干干操 | 日本色视频 | 国产又爽 又黄 在线 | 悠悠久久伊人闲闲红红综合 | 嗯啊哦好爽插我h | 骚货无码 | 嫩操影视 |