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
RecursionAll root-to-leaf paths are:
["1->2->5", "1->3"]
復(fù)雜度
O(N), O(H)
思路
基本算法,遞歸。
代碼
public ListIterationbinaryTreePaths(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); }
復(fù)雜度
O(N), O(N)
思路
遞歸用stack和stack進(jìn)行dfs。
代碼
public Listpaths(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
摘要:在線網(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...
摘要:月下半旬攻略道題,目前已攻略題。目前簡(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ū)別...
摘要:遞歸法復(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...
摘要:解題思路利用遞歸,對(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...
摘要: 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...
閱讀 1274·2021-11-23 09:51
閱讀 1635·2021-11-16 11:45
閱讀 4060·2021-10-09 09:43
閱讀 2694·2021-07-22 16:47
閱讀 953·2019-08-27 10:55
閱讀 3456·2019-08-26 17:40
閱讀 3098·2019-08-26 11:39
閱讀 3238·2019-08-23 18:39