We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f2fe500 commit 3fdcbe2Copy full SHA for 3fdcbe2
kotlin/0112-path-sum.kt
@@ -0,0 +1,21 @@
1
+/**
2
+ * Example:
3
+ * var ti = TreeNode(5)
4
+ * var v = ti.`val`
5
+ * Definition for a binary tree node.
6
+ * class TreeNode(var `val`: Int) {
7
+ * var left: TreeNode? = null
8
+ * var right: TreeNode? = null
9
+ * }
10
+ */
11
+class Solution {
12
+ fun hasPathSum(root: TreeNode?, targetSum: Int): Boolean {
13
+
14
+ root?: return false
15
16
+ if(root.left == null && root.right == null) return targetSum - root.value == 0
17
+ else return hasPathSum(root.left, targetSum - root.value) || hasPathSum(root.right, targetSum - root.value)
18
+ }
19
20
+ val TreeNode.value get() = this.`val`
21
+}
0 commit comments