LeetCode OJ 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.
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.
Subscribe to see which companies asked this question
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ int flag = false; void DFS(struct TreeNode *root, int sum){ if(root == NULL){ return; } ){ flag = true; } DFS(root->left, sum - root->val); DFS(root->right, sum - root->val); } bool hasPathSum(struct TreeNode* root, int sum) { flag = false; DFS(root, sum); return flag; }
LeetCode OJ 112. Path Sum的更多相关文章
- 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 OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Path Sum
Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...
- 【一天一道LeetCode】#112. Path Sum
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- 【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 ...
- LeetCode OJ:Path Sum II(路径和II)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- LeetCode OJ: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】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- JAVA基础知识:容器
JDK所提供的容器都在java.util包里面,下面开始讨论的都是JDK1.4版本的,只讲述基本知识,不涉及泛型 容器API的类图结构如下图所示 Set:元素无顺序且不可重复 List:元素 ...
- source tree 推送错误解决
fatal: The remote end hung up unexpectedly 出现这个问题是因为文件过大 解决办法: 打开git bash 输入git config --global http ...
- 用 pyvenv 创建几个不相互影响的python虚拟环境
IN MY UBUNTU python2的环境控制: sudo apt-get install virtualenv 创建: virtualenv --no-site-packages [环境搭建目 ...
- iOS静态分析举例
XCode-> Product -> Analyze 即可进行iOS静态代码分析.静态分析能发现的问题包括以下几种类型: 1.逻辑错误:访问空指针或未初始化的变量等: 2.内存管理错误:如 ...
- AX7: Install a deployable package
Table of Contents Introduction Key concepts Collect topology configuration data Generate a runbook f ...
- 在Ubuntu上安装Mysql For Python
安装: 首先安装pip,并且把pip更新到最小版本 apt-get install python-pip pip install -U pip 安装mysql开发包 apt-get install p ...
- 自动获取MyEcilipse注册名和注册码的方法
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; //在MyEc ...
- 第六百一十二、三、四、五天 how can I 坚持
上火了啊..已经2017了,真快,人生只是一瞬间,到头来都是一场空. 2016年最后一天,很悲催,没赶上火车.还不能改签,哪能哪次都这么幸运,长记性了,下午到济南,看了看弟弟的房子,到挺不错,就是装修 ...
- egret.Tween、egret.Ease
循环调用.只能设置boolean,不能设置循环次数. egret.Tween.).call(()=>{ console.log("循环调用"); }) 每次改变时,调用onC ...
- XML.04-dom4j和XPath
body,td { font-family: calibri; font-size: 10pt } XML.04-dom4j和XPath dom4j的基本使用 XPath 啥是XPath XPath语 ...