[Swift]LeetCode114. 二叉樹展開爲鏈表 | Flatten Binary Tree to Linked List

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

Given a binary tree, flatten it to a linked list in-place.node

For example, given the following tree:git

    1
   / \
  2   5
 / \   \
3   4   6

The flattened tree should look like:github

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6

給定一個二叉樹,原地將它展開爲鏈表。微信

例如,給定二叉樹app

    1
   / \
  2   5
 / \   \
3   4   6

將其展開爲:spa

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6

20ms
 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 flatten(_ root: TreeNode?) {
16         guard let root = root else { return }
17         var l = root.left != nil;
18         var r = root.right != nil;
19         flatten(root.left)
20         flatten(root.right)
21         if (l && r) {
22             var left = root.left!;
23             let right = root.right;
24             root.right = left;
25             root.left = nil;
26             while (left.right != nil) {
27                 left = left.right!;
28             }
29             left.right = right;
30         } else if (l) {
31             root.right = root.left;
32             root.left = nil;
33         }
34     }
35 }

20mscode

 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 flatten(_ root: TreeNode?) {
16         if root == nil {
17             return
18         } else {
19             flatten(root!.left)
20             flatten(root!.right)
21             if var node = root!.left {
22                 while node.right != nil {
23                     node.left = nil
24                     node = node.right!
25                 }
26                 node.right = root!.right
27                 root!.right = root!.left
28                 root!.left = nil
29             }
30         }
31     }
32 }

24mshtm

 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     var pre : TreeNode?
16     func flatten(_ root: TreeNode?) {
17         if root == nil{
18             return
19         }
20         flatten(root?.right)
21         flatten(root?.left)
22         root?.right = pre
23         root?.left = nil
24         pre = root
25     }
26 }

28msblog

 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 flatten(_ root: TreeNode?) {
16         helper(root)
17     }
18     
19     private func helper(_ node: TreeNode?) -> TreeNode? {
20         var node = node
21         if node == nil {
22             return node
23         }
24         if node!.left == nil && node!.right == nil {
25             return node
26         }
27         
28         let left = node!.left, right = node!.right
29         node!.left = nil
30         
31         if let left = left {
32             node!.right = left
33             node = helper(left)
34         }
35         if let right = right {
36             node!.right = right
37             node = helper(right)
38         }
39         
40         return node
41     }
42 }

 40ms

 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     var array = [Int]()
16     func flatten(_ root: TreeNode?) {
17         if(root == nil){ return }
18         traverse(root)
19         root!.left = nil
20         root!.right = nil
21         var currentNode = root
22         for index in 1 ..< array.count{
23             currentNode!.right = TreeNode(array[index])
24             currentNode = currentNode!.right
25         }
26     }
27     func traverse(_ root: TreeNode?){
28         if(root == nil){ return }
29         self.array.append(root!.val)
30         traverse(root!.left)
31         traverse(root!.right)
32     }
33 }
相關文章
相關標籤/搜索