
- 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 difference between node and a descendent in Python
Suppose we have a binary tree, we have to find the largest absolute difference between any node and its descendants.
So, if the input is like
then the output will be 7 as largest absolute difference is between nodes 8 and 1.
To solve this, we will follow these steps −
- Define a function dfs() . This will take node
- if node is not null, then
- return a list with positive and negative infinity
- left := dfs(left of node)
- right := dfs(right of node)
- res := a pair with (minimum of left[0], right[0] and value of node, and maximum of left[1], right[1] and value of node)
- ans := maximum of ans, (value of node - res[0]) and (res[1] - value of node)
- return res
- From the main method, do the following −
- ans := 0
- dfs(root)
- return ans
Let us see the following implementation to get better understanding −
Example
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 [float("inf"), float("-inf")] left = dfs(node.left) right = dfs(node.right) res = [min(left[0], right[0], node.val), max(left[1], right[1], node.val)] self.ans = max(self.ans, node.val - res[0], res[1] - node.val) return res self.ans = 0 dfs(root) return self.ans ob = Solution() root = TreeNode(1) root.left = TreeNode(5) root.right = TreeNode(3) root.right.left = TreeNode(2) root.right.right = TreeNode(8) root.right.left.left = TreeNode(7) root.right.left.right = TreeNode(4) print(ob.solve(root))
Input
root = TreeNode(1) root.left = TreeNode(5) root.right = TreeNode(3) root.right.left = TreeNode(2) root.right.right = TreeNode(8) root.right.left.left = TreeNode(7) root.right.left.right = TreeNode(4)
Output
7
- Related Articles
- Program to find leftmost deepest node of a tree in Python
- Program to find Kth ancestor of a tree node in Python
- Program to find second deepest node in a binary tree in python
- Python program to find difference between two timestamps
- Program to find sibling value of a binary tree node in Python
- Python program to find difference between current time and given time
- Maximum Difference Between Node and Ancestor in C++
- Program to find the middle node of a singly linked list in Python
- Python Program to Find Nth Node in the Inorder Traversal of a Tree
- Python program to find the maximum and minimum value node from a doubly linked list
- Python program to find the maximum and minimum value node from a circular linked list
- Program to find the K-th last node of a linked list in Python
- Python Program to Find All Nodes Reachable from a Node using BFS in a Graph
- Python program to find sum of absolute difference between all pairs in a list
- Program to find out the node in the right in a binary tree using Python

Advertisements