Path Sum 判断二叉树的和 DFS处理
来源:互联网 发布:下拉通刷词软件 编辑:程序博客网 时间:2024/05/21 18:48
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.
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //DFS处理class Solution {public: bool hasPathSum(TreeNode *root, int sum) { return dfs(root,sum,0); } bool dfs(TreeNode *node,int sum,int cur) { if(node==NULL) return false; if(node->left==NULL&&node->right==NULL) return sum==cur+node->val; return dfs(node->left,sum,cur+node->val)||dfs(node->right,sum,cur+node->val); }};
0 0
- Path Sum 判断二叉树的和 DFS处理
- 【二叉树】DFS计算路径和,保存路径【113. Path Sum II】【112. Path Sum】
- LeetCode (12) Path Sum (二叉树路径和判断)
- leetcode系列(24)Path Sum 判断二叉树中和为Sum的路径是否存在
- 【DFS&二叉树】Binary Tree Maximum Path Sum
- [LeetCode 124] Binary Tree Maximum Path Sum (DFS/二叉树)
- Sum Root to Leaf Numbers 二叉树的path 和
- Path Sum 二叉树路径和 @LeetCode
- leetcode 113. Path Sum II 二叉树求符合和值为sum的链
- [LeetCode]437. Path Sum III(求二叉树中路径和等于sum的数量)
- 112:Path Sum【树】【DFS】
- [LeetCode]112. Path Sum(判断二叉树根到叶路径和是否等于sum)
- leetcode_112. Path Sum 二叉树路径求和,判断是否存在路径和为某个值的路径,深度遍历,递归的return
- 【leetcode 二叉树路径和】Path Sum 和 Path Sum II
- 二叉树 maximum path sum.
- 二叉树系列---path-sum
- 【二叉树】112. Path Sum
- Binary Tree Maximum Path Sum 二叉树最大路径和
- 黑马程序员——Java基础知识——泛型、枚举
- Android微信第三方登录接入
- android 输入法显示和隐藏监听
- hessian源码分析
- zoj - 2928 - Mathematical contest in modeling(爬山)
- Path Sum 判断二叉树的和 DFS处理
- ffplay 播放字幕以及lrc歌词
- Qt Creator Window平台的安装
- 什么是sys文件系统
- 在WinDBG中查看调用栈的命令
- java.lang.ClassCastException: com.sun.proxy.$Proxy* cannot be cast to***问题解决方案
- Pascal's Triangle 打印数字三角形
- android颜色对应的xml配置值,颜色表
- 内核的makefile文档 2.6.30 Kernel