Given a Binary tree, The task is to find the maximum diff obtained by subtracting a descendant's value from an ancestor's value.
Examples:
Input:
Output: 7
Explanation: We can have various ancestor-node difference, some of which are given below :
8 – 3 = 5 , 3 – 7 = -4, 8 – 1 = 7, 10 – 13 = -3
Among all those differences maximum value is 7 obtained by subtracting 1 from 8, which we need to return as result.Input:
9
/ \
6 3
/ \
1 4
Output: 8
The idea is to perform a recursive traversal of the binary tree, where for each node, we keep track of the maximum value encountered from the root to that node (i.e., its ancestors), and compute the difference between this maximum and the current node's value to get the potential maximum difference where the ancestor has a larger value than the descendant.
Step by step approach:
- Start recursion from root node and pass maximum node value ( Integer Minimum as there is no ancestor of root node).
- For each node, compute the difference between maximum value ancestor node and current node.
- Recursively compute the maximum difference for left subtree, and pass maximum value traversed so.
- Similarly recur for right subtree.
- Return the maximum of the three values.
// C++ program to find Maximum difference
// between node and its ancestor in Binary Tree
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node (int x) {
data = x;
left = nullptr;
right = nullptr;
}
};
// Function to find the maximum difference
// between nodes A and B, where A is an
// ancestor of B.
int maxDiffRecur(Node* root, int maxi) {
// Base Case:
if (root == nullptr) return INT_MIN;
// Calculate difference of maximum value
// ancestor and current node
int val = (maxi != INT_MIN) ? maxi - root->data : maxi;
// Find maximum difference in left subtree
int left = maxDiffRecur(root->left, max(maxi, root->data));
// Find maximum difference in right subtree
int right = maxDiffRecur(root->right, max(maxi, root->data));
// Return maximum of 3 values
return max({val, left, right});
}
int maxDiff(Node* root) {
return maxDiffRecur(root, INT_MIN);
}
int main() {
// Hard coded binary tree
// 8
// / \
// 3 10
// / \ \
// 1 6 14
// / \ /
// 4 7 13
Node* root = new Node(8);
root->left = new Node(3);
root->right = new Node(10);
root->left->left = new Node(1);
root->left->right = new Node(6);
root->left->right->left = new Node(4);
root->left->right->right = new Node(7);
root->right->right = new Node(14);
root->right->right->left = new Node(13);
cout << maxDiff(root);
return 0;
}
// Java program to find Maximum difference
// between node and its ancestor in Binary Tree
class Node {
int data;
Node left;
Node right;
Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Function to find the maximum difference
// between nodes A and B, where A is an
// ancestor of B.
static int maxDiffRecur(Node root, int maxi) {
// Base Case:
if (root == null) return Integer.MIN_VALUE;
// Calculate difference of maximum value
// ancestor and current node
int val = (maxi != Integer.MIN_VALUE) ? maxi - root.data : maxi;
// Find maximum difference in left subtree
int left = maxDiffRecur(root.left, Math.max(maxi, root.data));
// Find maximum difference in right subtree
int right = maxDiffRecur(root.right, Math.max(maxi, root.data));
// Return maximum of 3 values
return Math.max(val, Math.max(left, right));
}
static int maxDiff(Node root) {
return maxDiffRecur(root, Integer.MIN_VALUE);
}
public static void main(String[] args) {
// Hard coded binary tree
// 8
// / \
// 3 10
// / \ \
// 1 6 14
// / \ /
// 4 7 13
Node root = new Node(8);
root.left = new Node(3);
root.right = new Node(10);
root.left.left = new Node(1);
root.left.right = new Node(6);
root.left.right.left = new Node(4);
root.left.right.right = new Node(7);
root.right.right = new Node(14);
root.right.right.left = new Node(13);
System.out.println(maxDiff(root));
}
}
# Python program to find Maximum difference
# between node and its ancestor in Binary Tree
import sys
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# Function to find the maximum difference
# between nodes A and B, where A is an
# ancestor of B.
def maxDiffRecur(root, maxi):
# Base Case:
if root is None:
return -sys.maxsize - 1
# Calculate difference of maximum value
# ancestor and current node
val = maxi - root.data if maxi != -sys.maxsize - 1 else -sys.maxsize - 1
# Find maximum difference in left subtree
left = maxDiffRecur(root.left, max(maxi, root.data))
# Find maximum difference in right subtree
right = maxDiffRecur(root.right, max(maxi, root.data))
# Return maximum of 3 values
return max(val, left, right)
def maxDiff(root):
return maxDiffRecur(root, -sys.maxsize - 1)
if __name__ == "__main__":
# Hard coded binary tree
# 8
# / \
# 3 10
# / \ \
# 1 6 14
# / \ /
# 4 7 13
root = Node(8)
root.left = Node(3)
root.right = Node(10)
root.left.left = Node(1)
root.left.right = Node(6)
root.left.right.left = Node(4)
root.left.right.right = Node(7)
root.right.right = Node(14)
root.right.right.left = Node(13)
print(maxDiff(root))
// C# program to find Maximum difference
// between node and its ancestor in Binary Tree
using System;
class Node {
public int data;
public Node left;
public Node right;
public Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Function to find the maximum difference
// between nodes A and B, where A is an
// ancestor of B.
static int maxDiffRecur(Node root, int maxi) {
// Base Case:
if (root == null) return int.MinValue;
// Calculate difference of maximum value
// ancestor and current node
int val = (maxi != int.MinValue) ? maxi - root.data : maxi;
// Find maximum difference in left subtree
int left = maxDiffRecur(root.left, Math.Max(maxi, root.data));
// Find maximum difference in right subtree
int right = maxDiffRecur(root.right, Math.Max(maxi, root.data));
// Return maximum of 3 values
return Math.Max(val, Math.Max(left, right));
}
static int maxDiff(Node root) {
return maxDiffRecur(root, int.MinValue);
}
public static void Main(string[] args) {
// Hard coded binary tree
// 8
// / \
// 3 10
// / \ \
// 1 6 14
// / \ /
// 4 7 13
Node root = new Node(8);
root.left = new Node(3);
root.right = new Node(10);
root.left.left = new Node(1);
root.left.right = new Node(6);
root.left.right.left = new Node(4);
root.left.right.right = new Node(7);
root.right.right = new Node(14);
root.right.right.left = new Node(13);
Console.WriteLine(maxDiff(root));
}
}
// JavaScript program to find Maximum difference
// between node and its ancestor in Binary Tree
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// Function to find the maximum difference
// between nodes A and B, where A is an
// ancestor of B.
function maxDiffRecur(root, maxi) {
// Base Case:
if (root === null) return Number.MIN_SAFE_INTEGER;
// Calculate difference of maximum value
// ancestor and current node
let val = (maxi !== Number.MIN_SAFE_INTEGER) ? maxi - root.data : maxi;
// Find maximum difference in left subtree
let left = maxDiffRecur(root.left, Math.max(maxi, root.data));
// Find maximum difference in right subtree
let right = maxDiffRecur(root.right, Math.max(maxi, root.data));
// Return maximum of 3 values
return Math.max(val, left, right);
}
function maxDiff(root) {
return maxDiffRecur(root, Number.MIN_SAFE_INTEGER);
}
// Hard coded binary tree
// 8
// / \
// 3 10
// / \ \
// 1 6 14
// / \ /
// 4 7 13
let root = new Node(8);
root.left = new Node(3);
root.right = new Node(10);
root.left.left = new Node(1);
root.left.right = new Node(6);
root.left.right.left = new Node(4);
root.left.right.right = new Node(7);
root.right.right = new Node(14);
root.right.right.left = new Node(13);
console.log(maxDiff(root));
Output
7
Time Complexity: O(n), for visiting every node of the tree.
Auxiliary Space: O(h) for recursion call stack.
