leetcode刷题

2017-03-22  本文已影响0人  rsliumin1994

<pre>

/**

* Definition for a binary tree node.

* 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) {

recursive( root,sum);

return res;

//

//

//没有选择直接跳出多层递归 可能会影响效率

}

bool res=false;

void recursive(TreeNode* root,int target)

{

if(root==NULL)

return;

if(root->left==NULL&&root->right==NULL&&target==(root->val))

{

res=true;

}

recursive(root->left,target-(root->val));

recursive(root->right,target-(root->val));

}

};

</pre>

上一篇 下一篇

猜你喜欢

热点阅读