
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find longest even value path of a binary tree in Python
Suppose we have a binary tree, we have to find the longest path consisting of even values between any two nodes in the tree.
So, if the input is like
then the output will be 5 as the longest path is [10, 2, 4, 8, 6].
To solve this, we will follow these steps −
ans := 0
Define a function find() . This will take node
if node is null, then
return (-1, -1)
leftCnt := maximum of returned value of find(left of node) + 1
rightCnt := maximum of returned value of find(right of node) + 1
if value of node is even, then
ans := maximum of ans and (leftCnt + rightCnt + 1)
return (leftCnt, rightCnt)
otherwise,
ans := maximum of ans, leftCnt and rightCnt
return (-1, -1)
From the main method do the following −
find(root)
return ans
Let us see the following implementation to get better understanding −
Example
class TreeNode: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def solve(self, root): ans = 0 def find(node): nonlocal ans if not node: return -1, -1 leftCnt = max(find(node.left)) + 1 rightCnt = max(find(node.right)) + 1 if node.val % 2 == 0: ans = max(ans, leftCnt + rightCnt + 1) return leftCnt, rightCnt else: ans = max(ans, max(leftCnt, rightCnt)) return -1, -1 find(root) return ans ob = Solution() root = TreeNode(2) root.left = TreeNode(10) root.right = TreeNode(4) root.right.left = TreeNode(8) root.right.right = TreeNode(2) root.right.left.left = TreeNode(6) print(ob.solve(root))
Input
root = TreeNode(2) root.left = TreeNode(10) root.right = TreeNode(4) root.right.left = TreeNode(8) root.right.right = TreeNode(2) root.right.left.left = TreeNode(6)
Output
5
- Related Questions & Answers
- 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 longest path with even sum in Python
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- Program to find longest path between two nodes of a tree in Python
- Longest ZigZag Path in a Binary Tree in C++
- Program to find largest sum of any path of a binary tree in Python
- Program to find sibling value of a binary tree node in Python
- Program to find length of the longest path in an n-ary tree in Python
- Program to find sum of all numbers formed by path of a binary tree in python
- Program to print the longest leaf to leaf path in a Binary tree using C++
- Program to find sum each of the diagonal path elements in a binary tree in Python
- Program to find length of longest matrix path length in Python
- Binary Tree Maximum Path Sum in Python
- Program to find top view of a binary tree in Python