File tree 3 files changed +63
-0
lines changed
3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ class TreeNode (object ):
2
+ def __init__ (self , x ):
3
+ self .val = x
4
+ self .left = None
5
+ self .right = None
6
+
7
+ class Solution (object ):
8
+ def hasPathSum (self , root , sum ):
9
+ """
10
+ :type root: TreeNode
11
+ :type sum: int
12
+ :rtype: bool
13
+ """
14
+ if root == None :
15
+ return False
16
+ if sum == root .val and root .left == None and root .right == None :
17
+ return True
18
+ return self .hasPathSum (root .left , sum - root .val ) or self .hasPathSum (root .right , sum - root .val )
Original file line number Diff line number Diff line change
1
+ //
2
+ // Leetcode_parctice
3
+ //
4
+ // Created by PixelShi on 2017/2/8.
5
+ // Copyright © 2017年 sfmDev. All rights reserved.
6
+ //
7
+
8
+ import UIKit
9
+
10
+ /**
11
+ * Definition for a binary tree node.
12
+ * public class TreeNode {
13
+ * public var val: Int
14
+ * public var left: TreeNode?
15
+ * public var right: TreeNode?
16
+ * public init(_ val: Int) {
17
+ * self.val = val
18
+ * self.left = nil
19
+ * self.right = nil
20
+ * }
21
+ * }
22
+ */
23
+
24
+ public class TreeNode {
25
+ public var val : Int
26
+ public var left : TreeNode ?
27
+ public var right : TreeNode ?
28
+ public init ( _ val: Int ) {
29
+ self . val = val
30
+ self . left = nil
31
+ self . right = nil
32
+ }
33
+ }
34
+
35
+ class Solution {
36
+ func hasPathSum( _ root: TreeNode ? , _ sum: Int ) -> Bool {
37
+ guard let root = root else {
38
+ return false
39
+ }
40
+ if sum == root. val && root. left == nil && root. right == nil {
41
+ return true
42
+ }
43
+ return hasPathSum ( root. left, sum - root. val) || hasPathSum ( root. right, sum - root. val)
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments