相关问题: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();
}
};

最新文章

  1. 在Linux虚拟机下配置tomcat
  2. 父子页面之间元素相互操作(iframe子页面)
  3. Mysql上手
  4. Mysql5.5源码安装步骤笔记记录
  5. Hangover[POJ1003]
  6. windows内核 内存管理
  7. TreeView 节点的显示,读取,操作
  8. jquery之hide()用法详解
  9. 使用Java进行MD5加密
  10. Remote Desktop manager 连接后无法自动登录
  11. SQL Server阻止了对组件xp_cmdshell过程的解决方案 分类: SQL Server 2015-03-05 08:31 305人阅读 评论(0) 收藏
  12. redirect的错误用法asp.net怎么使用自定义错误
  13. opkg
  14. 解决华为手机不打印Log信息的问题
  15. Servlet是否单例?
  16. CentOS7.5修改字符集
  17. 深度解析使用CSS单位px、em、rem、vh、vw、vmin、vmax实现页面布局
  18. phpcms调用指定文章内容模型的ID
  19. linux学习第三天 (Linux就该这么学)
  20. Spark 实践——用决策树算法预测森林植被

热门文章

  1. 一文简单理解package-lock.json
  2. Js不用for,forEach,map等循环实现九九乘法表
  3. shell编程注意点
  4. Python之面向对象之反射、内置方法
  5. Hibernate基本原理理解
  6. 关于AES加密CryptoJS
  7. try-catch-finally try中有rerun 是否执行finally
  8. 【leetcode】1210. Minimum Moves to Reach Target with Rotations
  9. DOM例子小结(一)
  10. C2MIF软件使用说明