亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Binary Tree Paths

李增田 / 2094人閱讀

摘要:解題思路求根節(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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.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 評論0 收藏0
  • [Leetcode] Binary Tree Paths 二叉樹路徑

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

    Vicky 評論0 收藏0
  • [Leetcode-Tree] Path Sum I II III

    摘要:解題思路利用遞歸,對于每個根節(jié)點(diǎn),只要左子樹和右子樹中有一個滿足,就返回每次訪問一個節(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 評論0 收藏0
  • [Leetcode] Path Sum I & II & III 路徑和1,2,3

    摘要:只要我們能夠有一個以某一中間路徑和為的哈希表,就可以隨時判斷某一節(jié)點(diǎn)能否和之前路徑相加成為目標(biāo)值。 最新更新請見: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 評論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...

    張金寶 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<