leetcode 112. Path Sum

/*
leetcode 112. Path Sum

Given 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值