Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1083 |

@bt8816103042 | 739 |

@SherlockHolmes3 | 444 |

@SHOAIBVIJAPURE | 430 |

@codeantik | 429 |

@shalinibhataniya1097 | 400 |

@ShamaKhan1 | 392 |

@neverevergiveup | 372 |

@amrutakashikar2 | 355 |

@murarry3625 | 350 |

@mahlawatep | 349 |

Complete Leaderboard |

Given a Binary Tree, you need to find the maximum value which you can get by subtracting the value of node B from the value of node A, where A and B are two nodes of the binary tree and A is an ancestor of B.

**Example 1:**

**Input:
** 5
/ \
2 1
**Output: **4**
Explanation:**The maximum difference we can
get is 4, which is bewteen 5 and 1.

**Example 2:**

**Input:
** 1
/ \
2 3
\
7
**Output: **-1**
Explanation:**The maximum difference we can
get is -1, which is between 1 and 2.

**Your Task:**

The task is to complete the function **maxDiff**() which finds the maximum difference between the node and its ancestor.

**Expected Time Complexity:** O(N).

**Expected Auxiliary Space:** O(H).

Note: H is the height of the tree.

**Constraints:**

1 <= Number of edges <= 10^{4}

0 <= Data of a node <= 10^{5}

**Note: **The **Input/Output** format and **Examples** given are used for the system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Maximum difference between node and its ancestor

...