LeetCode 112. Path Sum--递归

来源:互联网 发布:dnf扫拍软件 编辑:程序博客网 时间:2024/05/17 09:21

题目链接

112Path 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.

For example:
Given the below binary tree and sum = 22,
              5             / \            4   8           /   / \          11  13  4         /  \      \        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.


/** * 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) {        if (root == NULL) return false;        if (root->left == NULL && root->right == NULL) return sum == root->val ? true : false;        return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);    }};


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 牵挂图片 了无牵挂 牵挂歌曲 牵挂伍佰 牵挂你的我 歌曲牵挂 牵挂的拼音 无奈的牵挂 牵挂的近义词 牵挂你的人是我 男人牵挂女人会有这四种表现 牵挂的反义词 思念牵挂一个人短语 牵挂是什么意思 儿行千里妈牵挂 一世牵挂全集播放40集 男人牵挂女人哪四种表现 用牵挂造句3年级 牵挂造句子3年级 牵挂你的人是我原唱 多情的牵挂原唱 你是我一生的牵挂 小学三年级用牵挂造句 牵绊 情牵两世 牵丝戏歌词 我想牵着你的手 我想牵着你手 我想牵你手 牵丝戏简谱 顺手牵羊 我想牵着你的手走遍这地球 老公牵一条狗和我配 牵羊礼 左牵黄右擎苍 牵一发而动全身 情牵一线 网络一线牵 国民萌宝牵回家 我想就这样牵着你的手不放开