[Swift]LeetCode100. 相同的樹 | Same Tree

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

Given two binary trees, write a function to check if they are the same or not.node

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.git

Example 1:github

Input:     1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

Output: true

Example 2:微信

Input:     1         1
          /           \
         2             2

        [1,2],     [1,null,2]

Output: false

Example 3:app

Input:     1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

Output: false

給定兩個二叉樹,編寫一個函數來檢驗它們是否相同。ide

若是兩個樹在結構上相同,而且節點具備相同的值,則認爲它們是相同的。函數

示例 1:spa

輸入:       1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

輸出: true

示例 2:code

輸入:      1          1
          /           \
         2             2

        [1,2],     [1,null,2]

輸出: false

示例 3:

輸入:       1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

輸出: false

8ms
 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 isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
16         return (p == nil && q == nil) || (p?.val == q?.val) && isSameTree(p?.right, q?.right) && isSameTree(p?.left, q?.left)
17     }
18 }

12ms

 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 isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
16         if p?.val != q?.val {
17             return false
18         }
19         if p?.left == nil && q?.left == nil && p?.right == nil && q?.right == nil {
20             return true
21         }else{
22             return isSameTree(p?.left, q?.left) && isSameTree(p?.right, q?.right)
23         }
24     }
25 }

12ms

 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 isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
16         var queue: [(TreeNode?,TreeNode?)] = [(p,q)]
17         while !queue.isEmpty {
18             let (a,b) = queue.removeFirst()
19             if !isEquaNode(a,b) {
20                 return false
21             }
22             if let leftA = a?.left, let leftB = b?.left {
23                 queue.append((leftA, leftB))
24             }
25             if let rightA = a?.right, let rightB = b?.right {
26                 queue.append((rightA, rightB))
27             }
28         }
29         return true
30     }
31     
32     func isEquaNode(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
33         if p == nil && q == nil { return true }
34         if p == nil && q != nil { return false }
35         if p != nil && q == nil { return false }
36         return p!.val == q!.val &&
37         p!.left?.val == q!.left?.val &&
38         p!.right?.val == q!.right?.val
39     }
40 }

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 isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
16         
17        if p?.val != q?.val {
18             return false
19         }
20         if p?.left != nil{
21             if p?.left?.val == q?.left?.val{
22                 if !isSameTree(p?.left, q?.left){
23                     return false
24                 }
25             }else{
26                 return false
27             }
28         }else if q?.left != nil{
29             return false
30         }
31         if p?.right != nil {
32             if p?.right?.val == q?.right?.val{
33                 if !isSameTree(p?.right, q?.right){
34                     return false
35                 }
36             }else{
37                 return false
38             }
39         }else if q?.right != nil{
40             return false
41         }
42         return true;
43     }
44 }

24ms

 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 isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
16         //遞歸
17         if let p = p, let q = q 
18         {
19             return p.val == q.val && isSameTree(p.left,q.left)
20             && isSameTree(p.right,q.right)        
21         }
22         else
23         {
24             return p == nil && q == nil
25         }
26     }
27 }
相關文章
相關標籤/搜索