[Swift]LeetCode100. 相同的树 | Same Tree
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9697990.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了