美文网首页
leetcode刷题

leetcode刷题

作者: rsliumin1994 | 来源:发表于2017-03-22 15:12 被阅读0次

<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>

相关文章

网友评论

      本文标题:leetcode刷题

      本文链接:https://www.haomeiwen.com/subject/eouonttx.html