前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode-Easy 437. Path Sum III

Leetcode-Easy 437. Path Sum III

作者头像
致Great
发布2018-04-11 17:39:43
5990
发布2018-04-11 17:39:43
举报
文章被收录于专栏:程序生活程序生活

101. Symmetric Tree

  • 描述: 给定一个二叉树和一个目标和,求满足和为目标值的路径个数
  • 思路: dfs 深度优先搜索
  • 代码
代码语言:javascript
复制
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def pathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: int
        """
        self.res = 0
        self.helper(root,sum)
        return self.res
    
    def dfs(self,root,sum,count,res):
        if not root:
            return
        count += root.val
        if count == sum:
            self.res += 1
        self.dfs(root.left,sum,count,self.res)
        self.dfs(root.right,sum,count,self.res)
    
    def helper(self,root,sum):
        if not root:
            return
        self.dfs(root,sum,0,self.res)
        self.helper(root.left,sum)
        self.helper(root.right,sum)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.04.06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 101. Symmetric Tree
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档