craking the code interview all path sum python

Problem:

You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum to a given value. The path does not need to start or end at the root or a leaf.

This is actually an level order traversal problem. We need to store the level value together and then calculate the summation of level up and level down together.

Here is the same test case as above tree problems in cc150.

class ListNode:
    def __init__(self,val):
        self.val=val
        self.next=None

class TreeNode:
    def __init__(self,val):
        self.val=val
        self.left=None
        self.right=None

root=TreeNode(0)
root.left=TreeNode(1)
root.right=TreeNode(2)
root.left.left=TreeNode(3)
root.left.right=TreeNode(4)
root.left.left.left=TreeNode(5)
root.right.left=TreeNode(8)
root.right.right=TreeNode(9)

we build the function

def levelorder(root,solution,path,level,target):
    if root==None:
        return
    path[level]=root.val
    temp=[]
    i=level
    while i>=0:
        temp=temp+[path[i]]
        if sum(temp)==target:
            solution.append(temp)
        i-=1
    levelorder(root.left,solution,path,level+1,target)
    levelorder(root.right,solution,path,level+1,target)

def main():
    solution=[]
    path={}
    levelorder(root,solution,path,0,4)
    print solution

if __name__=="__main__":
    main()
时间: 2024-08-07 21:18:13

craking the code interview all path sum python的相关文章

[leetcode]Minimum Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or r

[leetcode]Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/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. For example:Given the below binary tree and sum =

[leetcode] path sum @ Python [recursion]

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. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

Project Euler 83:Path sum: four ways 路径和:4个方向

Path sum: four ways NOTE: This problem is a significantly more challenging version of Problem 81. In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, is indicated in bold red an

Project Euler 80:Path sum: two ways 路径和:两个方向

Path sum: two ways In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down, is indicated in bold red and is equal to 2427.           131 673 234 103 18 201 96 342 965 150 630 803 74

LeetCode_Binary Tree Maximum Path Sum

一.题目 Binary Tree Maximum Path Sum Total Accepted: 41576 Total Submissions: 193606My Submissions 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 Retu

leetcode_113题——Path Sum II(深度优先搜索)

Path Sum II Total Accepted: 41402 Total Submissions: 155034My Submissions Question Solution Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5

LintCode 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. Example Given the below binary tree: 1 / 2 3 return 6. For this problem we need to think about the problem in this way. Now we want the largest sum of

lintcode-medium-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. Given the below binary tree: 1 / 2 3 return 6. 一个二叉树的最大量路径,分三种情况: 1. 在左子树 2. 在右子树 3. 包括root,左子树一部分和右子树一部分 所以需要定义一个类,用来记录不同的情况,一个int用来记录左子树/右子树的最大值,一个i