国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Leetcode[257] Binary Tree Paths

liujs / 1294人閱讀

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"]

Recursion

復(fù)雜度
O(N), O(H)

思路
基本算法,遞歸。

代碼

public List binaryTreePaths(TreeNode root) {
    List list = new LinkedList<>();
    helper(root, "", list);
    return list;
}

public void helper(TreeNode root, String cur, List list) {
    if(root == null) return null;
    cur += root.val;
    if(root.left == null && root.right == null) {
        list.add(cur);
        return;
    }
    cur += "->";
    helper(root.left, cur, list);
    helper(root.right, cur, list);
}
Iteration

復(fù)雜度
O(N), O(N)

思路
遞歸用stack和stack進(jìn)行dfs。

代碼

public List paths(TreeNode root) {
    StringBuilder builder = new StringBuilder();
    List res = new LinkedList<>();
    if(root == null) return res;
    Stack stack = new Stack<>();
    Set set = new HashSet<>();
    stack.push(root);
    builder.append(root.val);
    while(!stack.isEmpty()) {
        TreeNode cur = stack.peek();
        set.add(cur);
        if(cur.left != null && !set.contains(cur.left)) {
            builder.append(cur.left.val);
            stack.push(cur.left);
            continue;
        }
        if(cur.right != null && !set.contains(cur.right)) {
            builder.append(cur.right.val);
            stack.push(cur.right);
            continue;
        }
        res.add(builder.toString());
        builder.deleteCharAt(builder.length() - 1);
    }
    return res;
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/65270.html

相關(guān)文章

  • 前端 | 每天一個(gè) LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號(hào)完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語(yǔ)言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號(hào): showImg(htt...

    張漢慶 評(píng)論0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月匯總(100 題攻略)

    摘要:月下半旬攻略道題,目前已攻略題。目前簡(jiǎn)單難度攻略已經(jīng)到題,所以后面會(huì)調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時(shí),攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚有什么區(qū)別...

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

    摘要:遞歸法復(fù)雜度時(shí)間空間遞歸棧空間對(duì)于二叉樹(shù)思路簡(jiǎn)單的二叉樹(shù)遍歷,遍歷的過(guò)程中記錄之前的路徑,一旦遍歷到葉子節(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),只要左子樹(shù)和右子樹(shù)中有一個(gè)滿足,就返回每次訪問(wèn)一個(gè)節(jié)點(diǎn),就將該節(jié)點(diǎn)的作為新的進(jìn)行下一層的判斷。代碼解題思路本題的不同點(diǎn)是可以不從開(kāi)始,不到結(jié)束。代碼當(dāng)前節(jié)點(diǎn)開(kāi)始當(dāng)前節(jié)點(diǎn)左節(jié)點(diǎn)開(kāi)始當(dāng)前節(jié)點(diǎn)右節(jié)點(diǎn)開(kāi)始 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)

    摘要: 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元查看
<