成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Binary Tree Paths

李增田 / 2045人閱讀

摘要:解題思路求根節(jié)點(diǎn)到葉子節(jié)點(diǎn)的路徑集合,采用深度搜索,利用遞歸實(shí)現(xiàn)。

Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

   1
 /   
2     3
 
  5

All root-to-leaf paths are:

["1->2->5", "1->3"]

1.解題思路

求根節(jié)點(diǎn)到葉子節(jié)點(diǎn)的路徑集合,采用深度搜索,利用遞歸實(shí)現(xiàn)。

2.代碼

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

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66265.html

相關(guān)文章

  • Leetcode[257] Binary Tree Paths

    LeetCode[257] Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are:[1->2->5, 1->3] ...

    liujs 評(píng)論0 收藏0
  • [Leetcode] Binary Tree Paths 二叉樹路徑

    摘要:遞歸法復(fù)雜度時(shí)間空間遞歸??臻g對(duì)于二叉樹思路簡單的二叉樹遍歷,遍歷的過程中記錄之前的路徑,一旦遍歷到葉子節(jié)點(diǎn)便將該路徑加入結(jié)果中。當(dāng)遇到最小公共祖先的時(shí)候便合并路徑。需要注意的是,我們要單獨(dú)處理目標(biāo)節(jié)點(diǎn)自身是最小公共祖先的情況。 Root To Leaf Binary Tree Paths Given a binary tree, return all root-to-leaf pat...

    Vicky 評(píng)論0 收藏0
  • [Leetcode-Tree] Path Sum I II III

    摘要:解題思路利用遞歸,對(duì)于每個(gè)根節(jié)點(diǎn),只要左子樹和右子樹中有一個(gè)滿足,就返回每次訪問一個(gè)節(jié)點(diǎn),就將該節(jié)點(diǎn)的作為新的進(jìn)行下一層的判斷。代碼解題思路本題的不同點(diǎn)是可以不從開始,不到結(jié)束。代碼當(dāng)前節(jié)點(diǎn)開始當(dāng)前節(jié)點(diǎn)左節(jié)點(diǎn)開始當(dāng)前節(jié)點(diǎn)右節(jié)點(diǎn)開始 Path SumGiven a binary tree and a sum, determine if the tree has a root-to-lea...

    notebin 評(píng)論0 收藏0
  • [Leetcode] Path Sum I & II & III 路徑和1,2,3

    摘要:只要我們能夠有一個(gè)以某一中間路徑和為的哈希表,就可以隨時(shí)判斷某一節(jié)點(diǎn)能否和之前路徑相加成為目標(biāo)值。 最新更新請(qǐng)見:https://yanjia.me/zh/2019/01/... Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addin...

    caiyongji 評(píng)論0 收藏0
  • [LeetCode] Path Sum (I & II & III)

    摘要: 112. Path Sum Problem Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node...

    張金寶 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<