[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS

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

Note: A leaf is a node with no children.

Example:

Input:

   1
 /   2     3
   5

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

Explanation: All root-to-leaf paths are: 1->2->5, 1->3

思路为DFS, 只是append进入stack的时候一并append进入当前的path即可.

1. Constraints1) can be empty

2. IdeasDFS     T: O(n)  S: O(n)1) edge case2) stack = [(root, str(root.val))]3) DFS, if leaf, ans.append(path)4)return ans

3. Codes3.1) iterable
 1 class Solution:
 2     def path(self, root):
 3         if not root: return []
 4         ans, stack = [], [(root, str(root.val))]
 5         while stack
 6             node, path = stack.pop()
 7             if not node.left and not node.right:
 8                 ans.append(path)
 9             if node.right:
10                 stack.append((node.right, path + "->" + node.right.val))
11             if node.left:
12                 stack.append((node.left, path + "->" + node.left.val))
13         return ans

3.2) Recursive

 1 class Solution:
 2     def path(self, root):
 3         def helper(root, path, ans):
 4             path += str(root.val)
 5             if not root.left and not root.right:
 6                 ans.append(path)
 7             if root.left:
 8                 stack.append((root.left, path + "->", ans))
 9             if root.right:
10                 stack.append((root.right, path + "->", ans))
11         if not root: return []
12         ans = []
13         helper(root, "", ans)
14         return ans

4.. Test cases

1) empty

2)  1

3)

   1
 /   2     3
   5

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9307968.html

时间: 2024-10-09 23:51:35

[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS的相关文章

leetCode 257. Binary Tree Paths 二叉树路径

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"] 思路: 1.采用二叉树的后序遍历非递归版 2.在叶子节点的时候处理字

Leetcode 257 Binary Tree Paths 二叉树 DFS

找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 p

(easy)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"] 思想:递归代码如下: /** * Definition for a binary tree node. * public class Tre

257. Binary Tree Paths [easy] (Python)

题目链接 https://leetcode.com/problems/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"] 题目翻译 给定一个二叉

[LeetCode] 437. Path Sum III_ Easy tag: DFS

You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to

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"] 题目标签:Tree 这道题目给了我们一个二叉树,让我们记录所有的路径,返回一个array string list. 我们可以另外设一个fin

Java [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"] 解题思路: 使用递归法. 代码如下: /** * Definition for a binary tree node. * pu

LeetCode:Binary Tree Paths - 获取一棵树从顶点到每个叶节点的路径

1.题目名称 Binary Tree Paths(获取一棵树从顶点到每个叶节点的路径) 2.题目地址 https://leetcode.com/problems/binary-tree-paths/ 3.题目内容 英文:Given a binary tree, return all root-to-leaf paths. 中文:给定一颗二叉树,返回所有的根节点到叶节点的路径 例如:现有一颗二叉树    1  /   2     3    5 所有由根节点到叶节点的路径如下: ["1->2-

<LeetCode OJ> 257. Binary Tree Paths

257. Binary Tree Paths My Submissions Question Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy 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: ["