Skip to content

Commit 98ab4a2

Browse files
committed
feat: add the solution of Same Tree(100) with kotlin.
1 parent 632bc46 commit 98ab4a2

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
| [070][070-question] | [Climbing Stairs][070-tips] | [][070-java] | | [][070-kotlin] |
4848
| [083][083-question] | [Remove Duplicates from Sorted List][083-tips] | [][083-java] | | [][083-kotlin] |
4949
| [088][088-question] | [Merge Sorted Array][088-tips] | [][088-java] | | [][088-kotlin] |
50-
| [100][100-question] | [Same Tree][100-tips] | [][100-java] | | |
50+
| [100][100-question] | [Same Tree][100-tips] | [][100-java] | | [][100-kotlin] |
5151
| [101][101-question] | [Symmetric Tree][101-tips] | [][101-java] | | |
5252
| [104][104-question] | [Maximum Depth of Binary Tree][104-tips] | [][104-java] | | |
5353
| [107][107-question] | [Binary Tree Level Order Traversal II][107-tips] | [][107-java] | | |
@@ -497,4 +497,5 @@
497497
[070-kotlin]: ./src/_070/kotlin/Solution.kt
498498
[083-kotlin]: ./src/_083/kotlin/Solution.kt
499499
[088-kotlin]: ./src/_088/kotlin/Solution.kt
500+
[100-kotlin]: ./src/_100/kotlin/Solution.kt
500501
[771-kotlin]: ./src/_771/kotlin/Solution.kt

src/_100/kotlin/Solution.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package _100.kotlin
2+
3+
import structure.TreeNode
4+
5+
/**
6+
* @author relish
7+
* @since 2018/04/25
8+
*/
9+
/**
10+
* Definition for a binary tree node.
11+
* class TreeNode(var `val`: Int = 0) {
12+
* var left: TreeNode? = null
13+
* var right: TreeNode? = null
14+
* }
15+
*/
16+
class Solution {
17+
fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean {
18+
if (p == null && q == null) return true
19+
if (p == null || q == null) return false
20+
return p.`val` == q.`val` && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
21+
}
22+
}
23+
24+
fun main(args: Array<String>) {
25+
val p = TreeNode(1)
26+
p.left = TreeNode(2)
27+
p.right = TreeNode(2)
28+
29+
val q = TreeNode(1)
30+
q.left = TreeNode(2)
31+
q.right = TreeNode(2)
32+
33+
println(Solution().isSameTree(p, q))
34+
}

tips/100/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Output: false
4949

5050
题意是比较两棵二叉树是否相同,那么我们就深搜比较各个节点即可。
5151

52+
java:
5253
```java
5354
/**
5455
* Definition for a binary tree node.
@@ -71,6 +72,16 @@ class Solution {
7172
}
7273
```
7374

75+
kotlin(184ms/100.00%):
76+
```kotlin
77+
class Solution {
78+
fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean {
79+
if (p == null && q == null) return true
80+
if (p == null || q == null) return false
81+
return p.`val` == q.`val` && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
82+
}
83+
}
84+
```
7485

7586
## 结语
7687

0 commit comments

Comments
 (0)