-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path112.cpp
More file actions
33 lines (29 loc) · 671 Bytes
/
112.cpp
File metadata and controls
33 lines (29 loc) · 671 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// 112.cpp
// LeetCode
//
// Created by 张佐玮 on 15/7/9.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Path Sum
//
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if (!root) {
return false;
}
if (!root -> left && !root -> right) {
return sum == root -> val;
}
return hasPathSum(root -> left, sum - root -> val) || hasPathSum(root -> right, sum - root -> val);
}
};