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 the values along the path equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22,
/ \
/ / \
/ \ \
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
方法一:利用深度优先搜索DFS的方法来遍历每一条完整的路径,利用递归方法不停地找子节点的左右结点。首先,若输入的是空节点,则返回false,若是为叶子结点且其值为当下的值时,则返回true,否则,只要子节点的左右结点中任一结点可行则返回true。(C++)
bool hasPathSum(TreeNode* root, int sum) {
if(!root)
return false;
if(root->val==sum&&!root->left&&!root->right)
return true;
return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);
}
方法二:使用遍历的方法,每一个结点都加上其父结点的值,如果到叶子结点时,其值等于sum,就存在一条符合题意的路径,在这道题中,不一定非要右结点先入栈,左右顺序不影响结果。(C++)
bool hasPathSum(TreeNode* root, int sum) {
if(!root)
return false;
stack<TreeNode*> s{{root}};
while(!s.empty()){
TreeNode* tmp=s.top();
s.pop();
if(!tmp->left&&!tmp->right){
if(tmp->val==sum)
return true;
}
if(tmp->right){
tmp->right->val+=tmp->val;
s.push(tmp->right);
}
if(tmp->left){
tmp->left->val+=tmp->val;
s.push(tmp->left);
}
}
return false;
}
LeetCode 112. Path Sum 二叉树的路径和 C++的更多相关文章
- [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 、 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 ...
- (二叉树 DFS 递归) 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]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 ...
- 112. Path Sum二叉树路径和
[抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- LeetCode 437. Path Sum III (路径之和之三)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
随机推荐
- Floyd(求每2个点之间的最短路)
稍微改变即可求传递闭包,即关心两点之间是否有同路: for(int i=0;i<n;i++) for(int j=0;j<n;j++){ if(i==j) d[i][i]=0; else ...
- 2018.5.11 B树总结
小结 B树:二叉树,每个结点只存储一个关键字,等于则命中,小于走左结点,大于 走右结点: B-树:多路搜索树,每个结点存储M/2到M个关键字,非叶子结点存储指向关键 字范围的子结点: 所有关键字在整颗 ...
- 离线安装expect
expect依赖tcl库 安装包: https://files.cnblogs.com/files/tozh/tcl8.4.11-src.tar.gz https://files.cnblogs.co ...
- sql注入-输入’or 1=1#
比如:在用户名输入框中输入:’or 1=1#,密码随便输入,这时候的合成后的SQL查询语句为: select * from users where username='' or 1=1#' a ...
- 反向Ajax:WebSocket
郭晨 软件151 1531610114 WebSocket 在HTML5中出现的WebSocket是一种比Comet还要新的反向Ajax技术,WebSocket启用了双向的全双工通信信道,许多浏览器( ...
- Git 基础和原理
Git 究竟是怎样的一个系统呢? 请注意接下来的内容非常重要,若你理解了 Git 的思想和基本工作原理,用起来就会知其所以然,游刃有余. 在开始学习 Git 的时候,请努力分清你对其它版本管理系统的已 ...
- [ZZ] 用matlab绘制箭头
用matlab绘制箭头 http://npfeng900.blog.163.com/blog/static/14456108201221922944998/ 用matlab绘制箭头1 用matlab绘 ...
- VS2015 使用GIT同步到github
https://www.cnblogs.com/newP/p/5732431.html(参考) 拉取(Pull):将远程版本库合并到本地版本库,相当于(Fetch+Meger) 获取(Fetch):从 ...
- IO练习
#IO操作 import time; fileObj = open('log.txt','a'); while(True): data = input('请输入要写入的内容 : '); if data ...
- 必须学会git和maven
转自: http://tieba.baidu.com/p/3458400116 很多人应该用过svn cvs之类的代码版本管理工具,git也是其中之一. svn和git最大的几个区别要点,svn必须要 ...