Grokking 75: Top Coding Interview Questions
Ask Author
Back to course home

0% completed

Solution: Maximum Difference Between Node and Ancestor
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

You are given the root of a binary tree. Each node in the tree has a unique value.

Find the maximum difference between the value of any node and the value of any ancestor of that node.

An ancestor of a node is any node on the path from the root to that node, excluding the node itself.

Examples

Example 1:

  • Input: [5, 3, 8, 1, 4, null, 10]
    5
   / \
  3   8
 / \   \
1   4  10

  • Output: 5
  • Explanation: The maximum difference is between node 10 and its ancestor node 5 (|10 - 5| = 5).

Example 2:

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible