为有牺牲多壮志,敢教日月换新天。

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9697990.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

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

Example 1:

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:

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

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

Output: false

给定两个二叉树,编写一个函数来检验它们是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例 1:

输入:       1         1
          / \       / \
         2   3     2   3

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

输出: true

示例 2:

输入:      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 }

 

posted @ 2018-09-25 09:36  为敢技术  阅读(345)  评论(0编辑  收藏  举报