相关问题:112 path sum

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
queue<TreeNode*> q;
int dp[];
q.push(root);
int index=;
int count=;
while(!q.empty()){
TreeNode* temp=q.front();
q.pop();
q.push(temp->left);
q.push(temp->right);
if(temp==NULL) dp[index++]=;
else dp[index++]=temp->val;
}
for(int i=;i<index;i++){
if(i==&&dp[i]==sum){
count++;
}else if(dp[i]<=&&dp[i]>=-){
dp[i]=dp[(i-)/]+dp[i];
if(dp[i]==sum) count++;
}
}
return count;
}
};

想使用层序遍历+动态规划的方法O(n)完成,被NULL节点不能加入queue<TreeNode*>  q给卡住了,之后再看看怎么改;对这种含有NULL多的怎么层序啊啊啊啊啊啊;

先看看大佬的方法:

python:非递归先序遍历+字典

 # Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def pathSum(self,root,sum):
if root==None:
return 0
from collections import defaultdict
dictionary =defaultdict(int)
dictionary[0]=1
pNode,curr_sum,stack,res,prev=root,0,[],0,None
while(len(stack) or pNode):
if pNode:
curr_sum+=pNode.val
stack.append([pNode,curr_sum])
dictionary[curr_sum]+=1
pNode=pNode.left
else:
pNode,curr_sum=stack[-1]
if pNode.right==None or pNode.right==prev:
res+=dictionary[curr_sum-sum]
if sum==0:
res-=1
dictionary[curr_sum]-=1
stack.pop()
prev=pNode
pNode=None
else:
pNode=pNode.right
return res

网上看的别人的C++代码:

原理:递归先序遍历,遍历的时候用vector记录根节点到每一个节点的路径,然后计算每一个父节点到当前节点的路径和,并判断与sum的值是否相等:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
int res=;
vector<TreeNode*> out;
dfs(root,sum,,out,res);
return res;
}
//递归先序遍历
void dfs(TreeNode* node,int sum,int curSum,vector<TreeNode*>& out,int &res){
if(!node) return;
curSum+=node->val;
//cout<<curSum<<endl;
if(curSum==sum) res++;
out.push_back(node);
int t=curSum;
for(int i=;i<out.size()-;i++){
t-=out[i]->val;
if(t==sum) res++;
}//以当前节点为末节点,利用vector回溯每一个父节点到当前节点的路径和;
dfs(node->left,sum,curSum,out,res);
dfs(node->right,sum,curSum,out,res);
out.pop_back();
}
};

leetcode 437 Path Sum III 路径和的更多相关文章

  1. [LeetCode] 437. Path Sum III 路径和 III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  2. 47. leetcode 437. Path Sum III

    437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...

  3. 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 ...

  4. 437 Path Sum III 路径总和 III

    给定一个二叉树,二叉树的每个节点含有一个整数.找出路径和等于给定数的路径总数.路径不需要从根节点开始,也不需要在叶节点结束,当路径方向必须是向下的(只从父节点到子节点).二叉树不超过1000个节点,节 ...

  5. 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 ...

  6. LeetCode 437. Path Sum III (STL map前缀和)

    找遍所有路径,特判以根为起点的串即可. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...

  7. [LeetCode] 113. 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 ...

  8. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  9. 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 ...

随机推荐

  1. javascript 与node的 event-loop

    参考:https://juejin.im/post/5c337ae06fb9a049bc4cd218 https://github.com/forthealllight/blog/issues/5 h ...

  2. 利用wampserve搭建本服务器

    1.官网下载安装包 注意:3.0.6版本需要下载依赖包vc依赖包 2.默认为英文 右击图标进入langue设置为中文 3.需要手动设置在现状态 右击=>选中wampsetting =>me ...

  3. Vmware 安装 ghost 版 win 7

    很早就弄过vmware,很可惜一直没有仔细研究过,这次要安装一个win7系统,重新又学一下了一下,下面说一下安装的操作步骤吧. 第一步,下载vmware,原版的下载地址就不说了,上传到百度网盘自己下载 ...

  4. Apache ab测试工具使用方法(无参、get传参、post传参)(转)

    转自Apache ab测试工具使用方法(无参.get传参.post传参) Ab测试工具是apache自带的测试工具,具有简单易上手的特性,下面我总结一下我的使用方法,首先去官方下载apache程序包, ...

  5. 001-SaltStack入门篇(一)之SaltStack部署

    早期运维工作中用过稍微复杂的Puppet,下面介绍下更为简单实用的Saltstack自动化运维的使用. Saltstack知多少Saltstack是一种全新的基础设施管理方式,是一个服务器基础架构集中 ...

  6. (转) Java中的负数及基本类型的转型详解

    (转) https://my.oschina.net/joymufeng/blog/139952 面这行代码的输出是什么? 下面两行代码的输出相同吗? 请尝试在Eclipse中运行上面的两个代码片段, ...

  7. Problem 1538 - B - Stones II 贪心+DP

    还是给你石头n枚,每一枚石头有两个值a和b,每取一个石头,除了这块石头其余所有的石头的a就都减去这个石头的b,问你取了的石头的a的总和最大可以为多少? 先按B从大到小排序 然后DP: 取的话:dp[i ...

  8. 【模板】Lucas定理

    代码如下 #include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=1e5+10 ...

  9. wordpress在线预览pdf插件

    插件名称:PDF.js Viewer Shortcode 插件主页:http://tphsfalconer.com/ 优点:功能强大,有分页缩略图功能,翻页,放大缩小,打印,下载,读取等功能. 使用方 ...

  10. 【leetcode】1184. Distance Between Bus Stops

    题目如下: A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between al ...