相关问题: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. Electron 5.0 发布

    Electron 5.0的主要变化 打包应用程序现在的行为与默认应用程序相同.将创建一个默认的应用程序菜单(除非应用程序有一个),并且将自动处理全部关闭窗口的事件. (除非应用程序处理事件) 现在默认 ...

  2. 文件I/O编程 (fcntl)

    Fcntl函数语法要点所需头文件:#include          #include          #include函数原型:int fcntl(int fd,cmd,struct flock ...

  3. zabbix 图形注释乱码

    1.寻找字体文件 1.1 首先需要找到zabbix后台的字体文件路径,字体文件的后缀为.ttf [root@zabbix ~]# cd /usr/share/zabbix/ [root@zabbix ...

  4. PAT Advanced 1041 Be Unique (20 分)

    Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...

  5. Binary Numbers AND Sum CodeForces - 1066E (前缀和)

    You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. You will r ...

  6. JAVA利用递归的方法删除一个文件夹以及文件夹下所有的子文件

    public static boolean deleteFolder(String url) { File file = new File(url); if (!file.exists()) { re ...

  7. thinkphp查询构造器和链式操作、事务

    插入 更新记录 查询数据 删除数据 插入数据----name这种用法,会去config.php中去寻找前缀,如果你定义了前缀tp,那么执行下条语句会查询对tp_data的插入操作 链式操作---> ...

  8. 第二章Python入门

    第二章 Python入门 2.1.简介 Python是著名的"龟叔"(Guido van Rossum)在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言 Pytho ...

  9. windows窗口启动redis

    必须先得配置好环境变量,才能在窗口开启 启动服务端:redis-server 启动客户端:redis-cli

  10. 多线程——Java中继承Thread类与实现Runnable接口的区别

    线程我只写过继承Thread类的,后来知道java多线程有三种方式,今天首先比较一下常用的继承Thread类和实现Runnable接口的区别. 按着Ctrl键进入Thread之后,发现Thread类也 ...