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

[Swift]LeetCode101.对称二叉树|SymmetricTree

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

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

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

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

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

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

 

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3

 

Note:
Bonus points if you could solve it both recursively and iteratively.


 

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

    1
   / \
  2   2
 / \ / \
3  4 4  3

但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

    1
   / \
  2   2
   \   \
   3    3

说明:

如果你可以运用递归和迭代两种方法解决这个问题,会很加分。


 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 isSymmetric(_ root: TreeNode?) -> Bool {
16         return checkTree(root, root)
17     }
18     
19     
20     private func checkTree(_ left: TreeNode?, _ right: TreeNode?) -> Bool {
21         if left == nil || right == nil { return left == nil && right == nil }
22         
23         if left?.val != right?.val { return false }
24         
25         return checkTree(left?.left, right?.right) && checkTree(left?.right, right?.left)
26     }
27 }

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 isSymmetric(_ root: TreeNode?) -> Bool {
16         return isSymmetric2(root)
17         
18         guard let root = root else { return true }
19         
20         var queue: [TreeNode] = []
21         if let left = root.left {
22             queue.append(left)
23         }
24         if let right = root.right {
25             queue.append(right)
26         }
27         
28         while !queue.isEmpty {
29             if queue.count % 2 != 0 {
30                 return false
31             }
32             let left = queue.removeFirst()
33             let right = queue.removeFirst()
34             
35             if left.val != right.val {
36                 return false
37             }
38             if left.left?.val != right.right?.val {
39                 return false
40             }
41             if left.right?.val != right.left?.val {
42                 return false
43             }
44             
45             if left.left != nil {
46                 queue.append(left.left!)
47             }
48             if right.right != nil {
49                 queue.append(right.right!)
50             }
51             if left.right != nil {
52                 queue.append(left.right!)
53             }
54             if right.left != nil {
55                 queue.append(right.left!)
56             }
57         }
58         return true
59     }
60     
61      func isSymmetric2(_ root: TreeNode?) -> Bool {
62         return _isSymmetric(root, root)
63     }
64     
65     func _isSymmetric(_ left: TreeNode?, _ right: TreeNode?) -> Bool {
66         if left == nil || right == nil {
67             return left == nil && right == nil
68         }
69         if left?.val != right?.val {
70             return false
71         }
72         return _isSymmetric(left?.left, right?.right) &&
73                _isSymmetric(left?.right, right?.left)
74     }
75 }

28ms

 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 
15 // create a matrix at each depth using a queue and bfs 
16 // walk through the levels to see if they are symmetric 
17 
18 
19 class Solution {
20     
21     func isSymmetric(_ root: TreeNode?) -> Bool {
22         guard let root = root else { return true }
23         var queue = [root]
24         var tree: [[Int?]] = [[root.val]]
25         
26         while queue.count > 0 {
27             
28             var newL = [Int?]()
29             
30             for _ in 0..<queue.count {
31                 let next = queue.first!
32                 queue.remove(at: 0)
33                 newL.append(next.left?.val)
34                 newL.append(next.right?.val)
35                 if let l = next.left {
36                     queue.append(l)
37                 }
38                 if let r = next.right {
39                     queue.append(r)
40                 }
41             }
42             
43             tree.append(newL)
44         }
45         
46         for i in tree {
47             var level = i 
48             while level.count > 1 {
49                 let first = level.first! 
50                 let last = level.popLast()
51                 level.remove(at: 0)
52                 if first != last {
53                     return false 
54                 }
55             } 
56         } 
57         
58         return true 
59     }
60 }

32ms

 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 isSymmetricTree(_ left: TreeNode?, _ right: TreeNode?) -> Bool {
16         if left == nil && right == nil {
17             return true
18         }
19         
20         if left == nil || right == nil {
21             return false
22         }
23         if left!.val == right!.val {
24             return isSymmetricTree(left?.left,right?.right) && isSymmetricTree(left?.right,right?.left)
25         }
26         return false
27     }
28 
29     func isSymmetric(_ root: TreeNode?) -> Bool {
30         if root == nil {
31             return true
32         }
33         return isSymmetricTree(root?.left, root?.right)
34     }
35 }

32ms

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

36ms

 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 
 8  *     public init(_ val: Int) {
 9  *         self.val = val
10  *         self.left = nil
11  *         self.right = nil
12  *     }
13  * }
14  */
15 class Solution {
16     func isSymmetric(_ root: TreeNode?) -> Bool {
17         return symmetric(root?.left, root?.right)
18     }
19     func symmetric(_ left : TreeNode? , _ right : TreeNode?)-> Bool{
20         switch (left, right) {
21         case let(left?, right?):
22             return left.val == right.val && symmetric(left.left, right.right) && symmetric(left.right, right.left)
23         case (nil, nil):
24             return true
25         default:
26             return false
27         }
28     }
29 }

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
iOS:将OC的NSMutableArray转为Swift的Array发布时间:2022-07-13
下一篇:
swift - 极光推送一些问题发布时间: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