leetcode 437 Path Sum III 路径和
相关问题: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 路径和的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 437 Path Sum III 路径总和 III
给定一个二叉树,二叉树的每个节点含有一个整数.找出路径和等于给定数的路径总数.路径不需要从根节点开始,也不需要在叶节点结束,当路径方向必须是向下的(只从父节点到子节点).二叉树不超过1000个节点,节 ...
- 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 ...
- LeetCode 437. Path Sum III (STL map前缀和)
找遍所有路径,特判以根为起点的串即可. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...
- [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 ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- 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 ...
随机推荐
- multipart/form-data提交
pip install requests-toolbelt from requests_toolbelt import MultipartEncoder import requests m = Mul ...
- 第99:真正理解拉格朗日乘子法和 KKT 条件
- 破解phpStorm 2018 亲测
网上教程很多,这里我就不多赘述,我也是看其他教程试过来的. 下面分步骤介绍一下: 1.下载,我这里选用的版本是 phpStorm 2018.3 ,下载地址 https://www.newasp.net ...
- 5月Linux市场Steam 份额在增长
随着新的一个月的开始,Valve公布了上个月的软件/硬件调查数据.在2019年5月,Steam Linux的使用率按百分比略微上升. 上个月,运行Linux的Steam用户比例(根据有争议的Steam ...
- JS基础知识二
JS控制语句 switch 语句用于基于不同的条件来执行不同的动作 <script> function myFunction(){ var x; var d=new Date().getD ...
- Interviewe HDU - 3486 (ST表+枚举 )(非二分,看下这个数据、)
YaoYao has a company and he wants to employ m people recently. Since his company is so famous, there ...
- tensorboard_scalar
import numpy as np from tensorboardX import SummaryWriter writer=SummaryWriter(log_dir="scala&q ...
- JSTL标签(转载)
JSTL标签是一个实现web功能的定制标签库,包括输出功能,条件判断,循环等,使用JSTL标签,为动态编写WEB应用程序提供了很大的方便性,能很好的和Java语言和HTML进行结合.下面我们看看jst ...
- Spring Boot 整合监听器
Listener是servlet规范中定义的一种特殊类,用于监听servletContext.HttpSession和servletRequest等域对象的创建和销毁事件,监听域对象的属性发生修改的事 ...
- python(Django2.0) 安装
前言 哇 ,python 是真的强大,看看如何安装的python: 下载咯 在python的官网下载python对应版本:https://www.python.org/downloads/window ...