[Swift]LeetCode257. 二叉樹的全部路徑 | Binary Tree Paths

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-zxbagioi-ma.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

Note: A leaf is a node with no children.git

Example:github

Input:

   1
 /   \
2     3
 \
  5

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

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

給定一個二叉樹,返回全部從根節點到葉子節點的路徑。微信

說明: 葉子節點是指沒有子節點的節點。app

示例:spa

輸入:

   1
 /   \
2     3
 \
  5

輸出: ["1->2->5", "1->3"]

解釋: 全部根節點到葉子節點的路徑爲: 1->2->5, 1->3

16ms
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func binaryTreePaths(_ root: TreeNode?) -> [String] {
16         var list:[String] = [String]()
17         recuesive(root,&list,String())
18         return list
19     }
20     func recuesive(_ root:TreeNode?,_ list:inout [String],_ str:String)
21     {
22         if root == nil {return}
23         var strNew:String = str
24         var strRoot:String = String(root!.val)
25         if root?.left == nil && root?.right == nil
26         {
27             strNew = strNew + strRoot
28             list.append(strNew)
29             return
30         }
31         strRoot = strNew + strRoot + "->"
32         recuesive(root?.left,&list,strRoot)
33         recuesive(root?.right,&list,strRoot)
34     }
35 }

16mscode

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func binaryTreePaths(_ root: TreeNode?) -> [String] {
16         guard let root = root else {
17             return []
18         }
19         var result = [String]()
20         binaryTreePathsDFS(root, "", &result)
21         return result
22     }
23     
24     func binaryTreePathsDFS(_ root: TreeNode, _ out: String, _ result: inout [String]) {
25         if root.left == nil && root.right == nil {
26             result.append(out + String(root.val))
27         }
28         
29         if root.left != nil {
30             binaryTreePathsDFS(root.left!, out + String(root.val) + "->", &result)
31         }
32         if root.right != nil {
33             binaryTreePathsDFS(root.right!, out + String(root.val) + "->", &result)
34         }
35         
36     }
37 }

16mshtm

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func binaryTreePaths(_ root: TreeNode?) -> [String] {
16         var ans = [String]()
17         binaryTreePaths(root, "", &ans)
18         return ans
19     }
20     
21     func binaryTreePaths(_ node: TreeNode?, _ path: String, _ ans: inout [String]) {
22         guard let node = node else { return }
23         
24         let path = path + String(node.val)
25         
26         if node.left == nil && node.right == nil {
27             ans.append(path)
28             return 
29         }
30         
31         binaryTreePaths(node.left, path + "->", &ans)
32         binaryTreePaths(node.right, path + "->", &ans)
33     }
34 }
相關文章
相關標籤/搜索