leetcode 112. Path Sum

来源:互联网 发布:tensorflow如何使用 编辑:程序博客网 时间:2024/06/03 20:22
/*leetcode 112. Path SumGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.题目大意:对于给定的二叉树判断是否有一个从根节点到子节点的路径,使得该路径的和等于给定的sum解题思路:只需返回true或false,因此只要找到了一个结果就返回。*/#include "TreeInclude.h"#include <queue>class Solution {public:    //DFS    bool hasPathSum1(TreeNode* root, int sum)     {        if (!root)            return false;        if ( !root->left && !root->right && root->val == sum)            return true;        else        {            bool left = hasPathSum(root->left, sum - root->val);            bool right = hasPathSum(root->right, sum - root->val);            return (left | right);        }        return false;    }    //非递归    bool hasPathSum(TreeNode* root, int sum)    {        if (!root)            return false;        queue<TreeNode*> q;        q.push(root);        TreeNode* cur;        while (q.size())        {            int size = q.size();            while (size--)            {                cur = q.front();                q.pop();                if (cur->left != NULL)                {                    q.push(cur->left);                    cur->left->val += cur->val;                }                if (cur->right != NULL)                {                    q.push(cur->right);                    cur->right->val += cur->val;                }                if (!cur->left && !cur->right && cur->val == sum)                    return true;            }        }        return false;    }};void TEST(){    Solution sol;    vector<int> v{ 5,4,8,11,13,4,7,2,-1,-1,-1,1 };    TreeNode* root = CreateTree(v);    cout << sol.hasPathSum(root, 22) << endl;    cout << sol.hasPathSum(root, 17) << endl;}int main(){    TEST();    return 0;}

TreeInclude.h:测试用

#ifndef _TREE_INCLUDE_H_ #define _TREE_INCLUDE_H_#include <iostream>#include <vector>#include <algorithm>#include <string>using namespace std;// 节点数据结构struct TreeNode{    int val;    TreeNode *left;    TreeNode *right;    TreeNode(int x) : val(x), left(NULL), right(NULL) {}};//使用数组创建二叉树void CreateNode(const vector<int>& v, TreeNode* cur, int curIndex){    int len = v.size();    if (len == 0 || cur == NULL || curIndex < 0 || curIndex >= len)        return;    if ((2 * curIndex + 1 < len) && (v[2*curIndex+1] != -1))    {        TreeNode* tmp = new TreeNode(v[2 * curIndex + 1]);        cur->left = tmp;    }    else        cur->left = NULL;    if ((2 * curIndex + 2 < len) && (v[2*curIndex+2] != -1))    {        TreeNode* tmp = new TreeNode(v[2 * curIndex + 2]);        cur->right = tmp;    }    else        cur->right = NULL;    CreateNode(v, cur->left, 2 * curIndex + 1);    CreateNode(v, cur->right, 2 * curIndex + 2);}TreeNode* CreateTree(const vector<int>& v){    int len = v.size();    if (len == 0)        return NULL;    TreeNode* root = new TreeNode(v[0]);    CreateNode(v, root, 0);    return root;}void FirstOrderTraverse(TreeNode* root){    if (root == NULL)        return;    cout << root->val << " ";    FirstOrderTraverse(root->left);    FirstOrderTraverse(root->right);    //cout << endl;}void MakeEmpty(TreeNode* root){    if (root != NULL)    {        MakeEmpty(root->left);        MakeEmpty(root->right);        delete root;    }}#endif // 
0 0
原创粉丝点击