leetcode之Path Sum II
来源:互联网 发布:js移动端日期选择插件 编辑:程序博客网 时间:2024/05/22 00:56
相关的几道题我想用同一种方法做出来,就是把所有的根到叶的路径全部打出来,形成一个list。然后再对list进行操作。不见得最快,但是最好想。代码如下:
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """ if not root: return [] pathlist = [] def showpath(list1, root): list2 = list1[::] if list2 == []: list2.append(root.val) else: list2.append(root.val) if not root.left and not root.right: pathlist.append(list2) else: if root.left: list1 = list2 showpath(list1, root.left) if root.right: list1 = list2 showpath(list1, root.right) return pathlist s = showpath([], root) pathlist1 = [] for i in s: sumofall = 0 for j in i: sumofall += j if sumofall == sum: pathlist1.append(i) return pathlist1
0 0
- leetcode之Path Sum II
- LeetCode 之 Path Sum II
- LeetCode之Path Sum II
- leetcode之Path Sum II
- leetcode之Path Sum II
- leetcode 之Path Sum II
- LeetCode之Path Sum II
- leetcode之Path Sum II
- LeetCode: Path Sum II
- LeetCode Path Sum II
- [Leetcode] Path Sum II
- LeetCode: Path Sum II
- [LeetCode] Path Sum II
- 【leetcode】Path Sum II
- [LeetCode]Path Sum II
- [Leetcode]Path Sum II
- [leetcode]Path Sum II
- Leetcode: Path Sum II
- linux系统下怎么输入EOF退出输入循环while(getline(cin, temp)){.....}
- 关于smarty引擎原理
- 关于访问权限控制的几点问题
- CSS3 3D转换入门篇
- Apache vhosts配置
- leetcode之Path Sum II
- shell基础知识
- tomcat配置之如何让用户访问时不需要输入项目名
- CNN 笔记
- windows下串口编程一(2015.12.29)
- Java Atomic变量
- UIKit框架 -UIView and frame和bounds之区别(位置和大小)
- 算法——排序专题
- 编译安装Nginx和pcre