Leetcoede 112 Path Sum 二叉树
二叉树的从叶子到根的和是否存在
/**
* Definition for binary tree
* 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)
{
return false;
}
if (!root->left && !root->right)
{
return sum == root->val;
}
return hasPathSum(root->left,sum - root->val)||hasPathSum(root->right,sum - root->val);
}
Leetcoede 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 ...
- 112. Path Sum二叉树路径和
[抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- LeetCode 112. Path Sum 二叉树的路径和 C++
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [LeetCode] Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- [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 ...
随机推荐
- ssh搭建后的简化
关于ssh如何搭建还有不懂得朋友可以参考以下网址:http://www.cnblogs.com/LarryBlogger/p/5841446.html 在这里我就不重复再讲了! ssh搭建后的简化 简 ...
- IIS mime类型 任意类型
HTTP头 任意mime类型 .* application/octet-stream
- apache端口的修改
apache 这个web服务默认在80端口监听...如果你访问一个网站 http://www.baidu.com 则默认一个端口是80 1. 一台机器可以有 1-65535 号端口 2. ...
- unreal3的viewport和client
名字里带viewport/client的类不少,以及相关的类: FViewportFrame.FViewport FViewportClient/UScriptViewportClient/UGame ...
- 9x25 串口映射
duart /dev/ttyS0 /dev/ttyS0 usart1 /dev/ttyS2 /dev/ttyS1 usart2 /dev/ttyS3 ...
- @gettrcname.sql
http://www.eygle.com/archives/2007/05/script_gettrcname.html 最近有很多朋友问起<深入浅出Oracle>一书中的一个脚本gett ...
- AIX 系统中 PVID 的含义与作用
网址: http://www.eygle.com/digest/2008/06/aix_pvid.html Pvid是aix系统中的ODM LVM用于识别PV的序列号,操作系统通过pvid来识别pv, ...
- cocos2d-x源码分析(1)
class CC_DLL CCCopying { public: virtual CCObject* copyWithZone(CCZone* pZone); }; class CC_DLL CCZo ...
- 用Backbone.js创建一个联系人管理系统(一)
原文 Build a Contacts Manager Using Backbone.js: Part 1 在这个教程里我们将会使用Backbone.js,Underscore.js,JQuery创建 ...
- js-特效部分学习-拖拽效果
一.客户区大小ClientWidth和ClientHeight <style> #box { width: 200px; height: 200px; background-color: ...