LeetCode 112. Path Sum 动态演示
给一个目标值,判断一棵树从根到叶子是否至少有一条路径加起来的和等于目标值
比较典型的深度优先算法。
引入一个全局变量bResult, 一旦找到一条,就不再搜索其他的了。
class Solution {
public:
void helper(TreeNode* cur, int sum, int target, bool& bResult){
if(bResult)
return; //a(cur)
//lk("root",cur)
//a(sum)
//a(target)
//dsp
if(!cur->left && !cur->right){
bResult|= target-sum==cur->val;
} if(cur->left)
helper(cur->left, sum+cur->val, target, bResult); if(cur->right)
helper(cur->right, sum+cur->val, target, bResult); } bool hasPathSum(TreeNode* root, int sum) {
if(!root)
return false; if(!root->left && !root->right){
return sum==root->val;
} //ahd(root)
bool bRet=false;
//a(bRet) if(root->left)
helper(root->left, root->val, sum, bRet); if(root->right)
helper(root->right, root->val, sum, bRet); //dsp
return bRet; }
};
程序运行动态演示 http://simpledsp.com/FS/Html/lc112.html
LeetCode 112. Path Sum 动态演示的更多相关文章
- 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] 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 ...
- [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 ...
- [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 (二叉树路径之和)
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
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 ----- java
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Java [Leetcode 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 -David_Lin
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- 小白学Python(15)——pyecharts 绘制树形图表 Tree
Tree-基本示例 import json import os from pyecharts import options as opts from pyecharts.charts import P ...
- linux相关(find/grep/awk/sed/rpm)
如何查找特定的文件: find :在指定目录下查找文件 find -name "filename" :从当前目录查找文件 find / -name "filename&q ...
- python实现不同条件下单据体的颜色不一样,比如直接成本分析表中关闭的细目显示为黄色
#引入clr运行库 import clr #添加对cloud插件开发的常用组件的引用 clr.AddReference('Kingdee.BOS') clr.AddReference('Kingdee ...
- idea 创建java web项目ssm-gradle
环境准备:jdk1.8+tomcat8+idea+gradle 1.创建项目SSM 使用gradle创建项目,按照提示如下 image.png 输入项目名称,组名 image.png im ...
- 启动ZOOKEEPER之后能查看到进程存在但是查不到状态,是因为。。。
一般我们在启动ZOOKEEPER之后能查看到进程并且能查到每个节点的状态,但是新手偶尔会遇到查不到状态的问题,这里主要说一下我自己遇到的问题. 是因为myid重复了.... 错误:总共三个节点,mas ...
- 1-基于Xilinx XCKU115的半高PCIe x8 硬件加速卡
基于Xilinx XCKU115的半高PCIe x8 硬件加速卡 一.概述 本板卡系我公司自主研发,采用Xilinx公司的XCKU115-3-FLVF1924-E芯片作为主处理器,主要用于FPGA硬件 ...
- ubuntu14彻底删除mysql!!!(精)
解决方法: 删除mysql前 先删除一下 /var/lib/mysql 还有 /etc/mysql sudo rm /var/lib/mysql/ -R sudo rm /etc/mysql/ -R ...
- ltp-ddt eth_switch_config学习
# @name ALE Table test using SWITCH-CONFIG # @desc Checks default entries in ALE table and verifies ...
- 【leetcode】1091. Shortest Path in Binary Matrix
题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top- ...
- java File过滤文件的多种方法
package com.qf.part1; import java.io.File; import java.io.FileFilter; import java.io.IOException; pu ...