-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
34 lines (33 loc) · 827 Bytes
/
Copy pathmain.cpp
File metadata and controls
34 lines (33 loc) · 827 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
34
//
// main.cpp
// leetcode112
//
// Created by 萧天牧 on 17/4/22.
// Copyright © 2017年 萧天牧. All rights reserved.
//
#include <iostream>
#include <queue>
using namespace std;
/**
* Definition for a binary tree node.
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
bool hasPathSum(TreeNode* root, int sum) {
if (!root)
return false;
if (root -> val == sum &&(!root -> left) && (!root -> right))
return true;
return hasPathSum(root -> left, sum - root -> val) || hasPathSum(root -> right,sum - root -> val );
}
int main(int argc, const char * argv[]) {
// insert code here...
TreeNode* root = new TreeNode(1);
root -> left = new TreeNode(2);
hasPathSum(root, 3);
return 0;
}