• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode100.相同的树|SameTree

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift UI专项训练3 第二个场景发布时间:2022-07-13
下一篇:
swift版的StringAttribute发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap