
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find length of longest path with even sum in Python
Suppose we have a binary tree. we have to find the length of the longest path whose sum is an even number.
So, if the input is like image, then the output will be 5, as the path is like [5, 2, 4, 8, 5], sum = 24(even).
To solve this, we will follow these steps −
- Define a function dfs() . This will take node
- if node is null, then
- return a pair (0, -inf)
- (left_0, left_1) := dfs(left of node)
- (right_0, right_1) := dfs(right of node)
- if value of node is odd, then
- ans := maximum of ans, (left_1 + right_0 + 1) and (left_0 + right_1 + 1)
- return a pair (maximum of (left_1 + 1), (right_1 + 1) and 0) , maximum of (left_0 + 1) and (right_0 + 1))
- otherwise,
- ans := maximum of ans, (left_0 + right_0 + 1) and (left_1 + right_1 + 1)
- return a pair (maximum of (left_0 + 1), (right_0 + 1), 0) , maximum of (left_1 + 1), (right_1 + 1))
- From the main method do the following −
- ans := 0
- dfs(root)
- return ans
Example (Python)
Let us see the following implementation to get better understanding −
class TreeNode: def __init__(self, data, left = None, right = None): self.val = data self.left = left self.right = right class Solution: def solve(self, root): def dfs(node): if not node: return 0, float("-inf") left_0, left_1 = dfs(node.left) right_0, right_1 = dfs(node.right) if node.val & 1: self.ans = max(self.ans, left_1 + right_0 + 1, left_0 + right_1 + 1) return max(left_1 + 1, right_1 + 1, 0), max(left_0 + 1, right_0 + 1) else: self.ans = max(self.ans, left_0 + right_0 + 1, left_1 + right_1 + 1) return max(left_0 + 1, right_0 + 1, 0), max(left_1 + 1, right_1 + 1) self.ans = 0 dfs(root) return self.ans ob = Solution() root = TreeNode(2) root.left = TreeNode(5) root.right = TreeNode(4) root.right.left = TreeNode(8) root.right.right = TreeNode(2) root.right.left.left = TreeNode(5) print(ob.solve(root))
Input
root = TreeNode(2) root.left = TreeNode(5) root.right = TreeNode(4) root.right.left = TreeNode(8) root.right.right = TreeNode(2) root.right.left.left = TreeNode(5)
Output
5
- Related Articles
- Program to find length of longest matrix path length in Python
- Program to find length of longest substring with even vowel counts in Python
- Program to find longest even value path of a binary tree in Python
- Program to find length of longest alternating path of a binary tree in python
- Program to find length of longest consecutive path of a binary tree in python
- Program to find length of the longest path in an n-ary tree in Python
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- Program to find length of longest sublist whose sum is 0 in Python
- Program to find length of the longest path in a DAG without repeated nodes in Python
- Program to length of longest increasing path in a given matrix in Python
- Program to find length of longest sublist with given condition in Python
- Program to find length of longest arithmetic subsequence with constant difference in Python
- Program to find length of longest consecutive sublist with unique elements in Python
- Program to find length of longest sublist with value range condition in Python
- Program to find length of longest balanced subsequence in Python

Advertisements