leetcode 437 路径和 Path Sum III

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int pathSum(TreeNode* root, int sum) {
13         queue<TreeNode*> q;
14         int dp[1000];
15         q.push(root);
16         int index=0;
17         int count=0;
18         while(!q.empty()){
19             TreeNode* temp=q.front();
20             q.pop();
21             q.push(temp->left);
22             q.push(temp->right);
23             if(temp==NULL) dp[index++]=1000010;
24             else dp[index++]=temp->val;
25         }
26         for(int i=0;i<index;i++){
27             if(i==0&&dp[i]==sum){
28                 count++;
29             }else if(dp[i]<=1000000&&dp[i]>=-1000000){
30                 dp[i]=dp[(i-1)/2]+dp[i];
31                 if(dp[i]==sum) count++;
32             }
33         }
34         return count;
35     }
36 };

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

先看看大佬的方法:

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

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def pathSum(self,root,sum):
10         if root==None:
11             return 0
12         from collections import defaultdict
13         dictionary =defaultdict(int)
14         dictionary[0]=1
15         pNode,curr_sum,stack,res,prev=root,0,[],0,None
16         while(len(stack) or pNode):
17             if pNode:
18                 curr_sum+=pNode.val
19                 stack.append([pNode,curr_sum])
20                 dictionary[curr_sum]+=1
21                 pNode=pNode.left
22             else:
23                 pNode,curr_sum=stack[-1]
24                 if pNode.right==None or pNode.right==prev:
25                     res+=dictionary[curr_sum-sum]
26                     if sum==0:
27                         res-=1
28                     dictionary[curr_sum]-=1
29                     stack.pop()
30                     prev=pNode
31                     pNode=None
32                 else:
33                     pNode=pNode.right
34         return res
35     

原文地址:https://www.cnblogs.com/joelwang/p/10453023.html

时间: 2024-09-30 12:02:27

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

LeetCode: Binary Tree Maximum Path Sum [124]

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.

leetcode第一刷_Minimum Path Sum

可以用递归简洁的写出,但是会超时. dp嘛.这个问题需要从后往前算,最右下角的小规模是已知的,边界也很明显,是最后一行和最后一列,行走方向的限制决定了这些位置的走法是唯一的,可以先算出来.然后不断的往前推算. 用distance[i][j]保存从当前位置走到最右下角所需的最短距离,状态转移方程是从distance[i+1][j]和distance[i][j+1]中选一个小的,然后再加上自身的. 代码很容易理解,这就是dp的魅力.空间上是可以优化的,因为当前状态只与后一行和后一列有关系. clas

[LeetCode] Binary Tree Maximum Path Sum(递归)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNo

求二叉树的路径和(path sum)

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, 1234567 5

LeetCode 437. Path Sum III(统计路径和等于sum的路径数量)

题意:统计路径和等于sum的路径数量. (1)节点值可正可负 (2)路径两端不一定是根结点或叶子结点 (3)路径一定是向下 分析:路径起点 (1)位于root(统计以root开头的和等于sum的路径数量) (2)位于root->left子树(递归) (3)位于root->right子树(递归) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode

[LeetCode] Binary Tree Maximum Path Sum(最大路径和)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 题目意思很简单,就是给定一棵二叉树,求最大路径和.path 可以从任意 node 开始,到任意 node 结束. 这道题在 LeetCode 上的通过率只有 20% 多一点,并被标记

[Leetcode] Binary tree maximum path sum求二叉树最大路径和

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return6. 思路:题目中说明起始节点可以是任意节点,所以,最大的路径和不一样要经过root,可以是左子树中某一条,或者是右子树中某一条,当然也可能是经过树的根节点root的.递归式是应该是这三者中

[LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 这道求二叉树的最大路径和是一道蛮有难度的题,难就难在起始位置和结束位置可以为任意位置,我当然是又不会了,于是上网看看大神们的解法,看了很多人的都没太看明白,最后发现了网友Yu's C

[Leetcode] Binary tree-- 437. Path Sum III

You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to