Skip to content

Commit 0110d19

Browse files
committed
feat: add the solution of Path Sum(112) with kotlin.
1 parent 3b5e4dd commit 0110d19

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
| [108][108-question] | [Convert Sorted Array to Binary Search Tree][108-tips] | [][108-java] | | [][108-kotlin] |
5555
| [110][110-question] | [Balanced Binary Tree][110-tips] | [][110-java] | | [][110-kotlin] |
5656
| [111][111-question] | [Minimum Depth of Binary Tree][111-tips] | [][111-java] | | [][111-kotlin] |
57-
| [112][112-question] | [Path Sum][112-tips] | [][112-java] | | |
57+
| [112][112-question] | [Path Sum][112-tips] | [][112-java] | | [][112-kotlin] |
5858
| [118][118-question] | [Pascal's Triangle][118-tips] | [][118-java] | | |
5959
| [119][119-question] | [Pascal's Triangle II][119-tips] | [][119-java] | | |
6060
| [121][121-question] | [Best Time to Buy and Sell Stock][121-tips] | [][121-java] | | |
@@ -508,4 +508,5 @@
508508
[108-kotlin]: ./src/_108/kotlin/Solution.kt
509509
[110-kotlin]: ./src/_110/kotlin/Solution.kt
510510
[111-kotlin]: ./src/_111/kotlin/Solution.kt
511+
[112-kotlin]: ./src/_112/kotlin/Solution.kt
511512
[771-kotlin]: ./src/_771/kotlin/Solution.kt

src/_112/kotlin/Solution.kt

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

tips/112/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ return true, as there exist a root-to-leaf path `5->4->11->2` which sum is 22.
2727

2828
题意是查找二叉树中是否存在从根结点到叶子的路径和为某一值,利用深搜在遇到叶子节点时判断是否满足即可。
2929

30-
30+
Java:
3131
```java
3232
/**
3333
* Definition for a binary tree node.
@@ -47,6 +47,16 @@ class Solution {
4747
}
4848
```
4949

50+
kotlin(216ms/100.00%):
51+
```kotlin
52+
class Solution {
53+
fun hasPathSum(root: TreeNode?, sum: Int): Boolean {
54+
if (root == null) return false
55+
if (root.left == null && root.right == null) return sum == root.`val`
56+
return hasPathSum(root.right, sum - root.`val`) || hasPathSum(root.left, sum - root.`val`)
57+
}
58+
}
59+
```
5060

5161
## 结语
5262

0 commit comments

Comments
 (0)