1. 程式人生 > >[Swift]LeetCode106. 從中序與後序遍歷序列構造二叉樹 | Construct Binary Tree from Inorder and Postorder Traversal

[Swift]LeetCode106. 從中序與後序遍歷序列構造二叉樹 | Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Return the following binary tree:

    3
   / \
  9  20
    /  \
   15   7

根據一棵樹的中序遍歷與後序遍歷構造二叉樹。

注意:
你可以假設樹中沒有重複的元素。

例如,給出

中序遍歷 inorder = [9,3,15,20,7]
後序遍歷 postorder = [9,15,7,20,3]

返回如下的二叉樹:

    3
   / \
  9  20
    /  \
   15   7

108ms
 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 buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode? { 16 let instart = 0
17 let inend = inorder.count - 1 18 let poststart = 0 19 let postend = postorder.count - 1 20 return buildTree(inorder, instart, inend, postorder, poststart, postend) 21 } 22 private func buildTree(_ inorder: [Int], _ instart: Int, _ inend: Int, _ postorder: [Int], _ poststart: Int, _ postend: Int )-> TreeNode? { 23 if instart > inend || poststart > postend { 24 return nil 25 } 26 let rootValue = postorder[postend] 27 let root = TreeNode(rootValue) 28 var k = 0 29 for i in 0..<inorder.count { 30 if inorder[i] == rootValue { 31 k = i 32 break 33 } 34 } 35 root.left = buildTree(inorder, instart, k - 1, postorder, poststart, poststart + k - 1 - instart) 36 root.right = buildTree(inorder, k + 1, inend, postorder, poststart + k - instart, postend - 1) 37 return root 38 } 39 }

116ms

 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 buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode? {
16          guard inorder.count > 0 && postorder.count > 0 && inorder.count == postorder.count else {
17             return nil
18         }
19         
20         return _buildHelper(inorder, 0, inorder.count - 1, postorder, 0, postorder.count - 1)
21     }
22     
23     func _buildHelper(_ inorder: [Int], _ inStart: Int, _ inEnd: Int, _ postorder: [Int], _ postStart: Int, _ postEnd: Int) -> TreeNode? {
24         guard inStart <= inEnd && postStart <= postEnd else {
25             return nil
26         }
27         
28         let root = TreeNode(postorder[postEnd])
29         
30         var mid = 0
31         for i in inStart...inEnd where inorder[i] == root.val {
32             mid = i
33             break
34         }
35         
36         root.left = _buildHelper(inorder, inStart, mid - 1, postorder, postStart, mid - 1 - inStart + postStart)
37         root.right = _buildHelper(inorder, mid + 1, inEnd, postorder, mid - inStart + postStart, postEnd - 1)
38         
39         return root
40     }
41 }

156ms

 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         // 根據中序和後序遍歷構建二叉樹
16     func buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode? {
17         if inorder.count <= 0 || postorder.count <= 0{
18             return nil
19         }
20         /*
21          *  後序遍歷的最後一個節點肯定是整個樹的根節點
22          */
23         return buildTrees(inorder, postorder)
24     }
25     
26     func buildTrees(_ inorder: [Int], _ postorder: [Int]) -> TreeNode{
27         
28          let postorderRight = postorder.count - 1
29         // 根據後續遍歷的最後一個節點作為根節點
30         let root = TreeNode.init(postorder[postorderRight])
31         
32         // 根據中序遍歷計算根節點左右子節點的個數
33         var indexRoot: Int = -1
34         
35         for index in 0..<inorder.count{
36             if inorder[index] == postorder[postorderRight]{
37                 indexRoot = index
38                 break
39             }
40         }
41         
42         // 左子節點的個數
43         let leftNum = indexRoot
44         // 右子節點的個數
45         let rightNum = postorderRight - indexRoot
46         
47         // 生成新的左右子樹
48         if indexRoot > 0{
49             let leftNode = buildTrees([Int](inorder[0...indexRoot - 1]), [Int](postorder[0...leftNum - 1]))
50             root.left = leftNode
51         }
52         if indexRoot < inorder.count - 1{
53             let rightNode = buildTrees([Int](inorder[indexRoot + 1...inorder.count - 1]), [Int](postorder[leftNum...leftNum + rightNum - 1]))
54             root.right = rightNode
55         }
56         
57         return root
58     }
59 }

156ms

 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 buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode? {
16         let len = postorder.count
17         if len == 0 { return nil }
18         let root = TreeNode(postorder.last!)
19         var i = 0
20         while i < len {
21             if inorder[i] == root.val {
22                 break
23             }
24             i += 1
25         }
26         
27         if i > 0 {
28             root.left = buildTree(Array(inorder[..<i]), Array(postorder[..<i]))
29         }
30         
31         if i < len-1 {
32             root.right = buildTree(Array(inorder[(i+1)...]), Array(postorder[i...(len-2)]))
33         }
34         
35         return root
36     }
37 }

248ms

 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 buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode? {
16         
17         if postorder.count == 0 {
18             return nil
19         }
20         
21         if postorder.count == 1 {
22             let val = postorder[postorder.count - 1]
23             let root = TreeNode(val)
24             return root
25         }
26         
27         let val = postorder[postorder.count - 1]
28         let root = TreeNode(val)
29         
30         var rootIndex = 0
31         for i in 0..<inorder.count {
32             if inorder[i] == val {
33                 rootIndex = i
34             }
35         }
36         
37         var tmpInorder = [Int]()
38         for i in 0..<rootIndex {
39             tmpInorder.append(inorder[i])
40         }
41         
42         
43         var tmpPostorder = [Int]()
44         for i in 0..<rootIndex {
45             tmpPostorder.append(postorder[i])
46         }
47         
48         root.left = buildTree(tmpInorder, tmpPostorder)
49     
50         tmpInorder = [Int]()
51         for i in rootIndex+1..<inorder.count {
52             tmpInorder.append(inorder[i])
53         }
54         
55         tmpPostorder = [Int]()
56         for i in rootIndex..<inorder.count - 1 {
57             tmpPostorder.append(postorder[i])
58         }
59         
60         root.right = buildTree(tmpInorder, tmpPostorder)
61         
62         return root
63     }
64 }

316ms

 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 buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode? {
16         guard inorder.count > 0 && postorder.count > 0 else { return nil }
17         let node = TreeNode(postorder.last!)
18         let index = inorder.firstIndex(of: node.val)!
19 
20         let inLeft = Array(inorder[0 ..< index])
21         let inRight = Array(inorder[index + 1 ..< inorder.count])
22 
23         let postLeft = Array(postorder[0 ..< index])
24         let postRight = Array(postorder[index ..< postorder.count - 1])
25 
26         node.left = buildTree(inLeft, postLeft)
27         node.right = buildTree(inRight, postRight)
28 
29         return node
30     }
31 }

328ms

 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 buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode? {
16         if inorder.count == 0 || postorder.count == 0 {
17             return nil
18         }
19         
20         let root = TreeNode(postorder[postorder.count - 1])
21         
22         guard let i = inorder.index(of: root.val) else { return nil }
23         if i <= 1 && i > 0 {
24             root.left = TreeNode(inorder[0])
25         } else if i > 1 {
26             root.left = buildTree(Array(inorder[0..<i]), Array(postorder[0..<i]))
27         }
28         
29         if i >= inorder.count - 2 && i < inorder.count - 1 {
30             root.right = TreeNode(inorder[inorder.count - 1])
31         } else if i <= inorder.count - 1 {
32             root.right = buildTree(Array(inorder[i + 1..<inorder.count]), Array(postorder[i..<postorder.count - 1]))
33         }
34         
35         return root
36     }
37 }

392ms

 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 buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode? {
16         guard !inorder.isEmpty && !postorder.isEmpty && inorder.count == postorder.count else {
17             return nil
18         }
19         
20         let root = TreeNode(postorder.last!)
21         let index = inorder.index(of: postorder.last!)!
22         root.left = buildTree([Int](inorder[..<index]), [Int](postorder[..<index]))
23         root.right = buildTree([Int](inorder[(index+1)...]), [Int](postorder[index..<(postorder.count-1)]))
24         
25         return root
26     }
27 }