GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — coding-problems/Trees/find_max_path_sum.py at master · lylaaa/coding-problems Expand file tree/ Copy pathfind_max_path_sum.pyMore file actions 96 lines (78 loc) · 2.38 KB / Copy pathfind_max_path_sum.pyFile metadata and controls96 lines (78 loc) · 2.38 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 ''' Find max path sum Wrie a function that takes a Binary Tree and returns its max path sum. Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: 18 Output explanation: 5 -> 2 -> 1 -> 3 -> 7 Input: -1 / \ -2 3 / \ / \ -4 -5 2 5 Output: 10 Output explanation: 2 -> 3 -> 5 ========================================= Traverse the tree and in each node compare create a new path where the left and right max subpaths are merging in the current node. Time Complexity: O(N) Space Complexity: O(N) , because of the recursion stack (but this is the tree is one branch), O(LogN) if the tree is balanced. ''' ############ # Solution # ############ # import TreeNode class from tree_helpers.py from tree_helpers import TreeNode def max_path_sum(tree): return find_max_path_sum(tree)[0] def find_max_path_sum(node): if node is None: return (0, 0) # get the result from the left subtree left_result = find_max_path_sum(node.left) # get the result from the right subtree right_result = find_max_path_sum(node.right) # create a new path by merging the max left and right subpaths current_path = left_result[1] + node.val + right_result[1] # find the max path till now, comparing the new path, max path from the left and right subtree max_path = max(left_result[0], current_path, right_result[0]) # find the max subpath, min value for a subpath sum is 0 max_subpath = max(left_result[1] + node.val, right_result[1] + node.val, node.val, 0) return (max_path, max_subpath) ########### # Testing # ########### # Test 1 # Correct result => 18 tree = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3, TreeNode(6), TreeNode(7))) print(max_path_sum(tree)) # Test 2 # Correct result => 10 tree = TreeNode(-1, TreeNode(-2, TreeNode(-4), TreeNode(-5)), TreeNode(3, TreeNode(2), TreeNode(5))) print(max_path_sum(tree)) # Test 3 ''' 1 / \ 7 3 / \ / \ -4 -5 6 2 ''' # Correct result => 17 (7 -> 1 -> 3 -> 6) tree = TreeNode(1, TreeNode(7, TreeNode(-4), TreeNode(-5)), TreeNode(3, TreeNode(6), TreeNode(2))) print(max_path_sum(tree)) # Test 4 ''' 1 / \ 2 3 / \ / \ -4 -5 -2 -3 ''' # Correct result => 6 (2 -> 1 -> 3) tree = TreeNode(1, TreeNode(2, TreeNode(-4), TreeNode(-5)), TreeNode(3, TreeNode(-2), TreeNode(-3))) print(max_path_sum(tree)) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |