6 kyu

Find amplitude of a binary tree

Description:

A binary tree is a data structure in which every node has at most two children (https://en.wikipedia.org/wiki/Binary_tree). A possible implementation in python of a binary tree is:

class Tree(object):
  
  def __init__(self, data, left=None, right=None):
    self.data = data
    self.left = left
    self.right = right

A path of a tree is a sequence of consecutive nodes, starting from the root node and ending in a leaf. The amplitude of a path is the biggest difference between two node values in a path. The amplitude of a tree is the biggest amplitude of all paths. An empty tree has the amplitude 0.

Consider this tree:

    5
   / \
  1   2
 / \
3   9

This tree has the paths [5, 1, 3] (amplitude 4), [5, 1, 9] (amplitude 8) and [5, 2] (amplitude 3). Hence, its amplitude is 8.

In this Kata you have to write a function which takes the root node of a tree (defined as in the example) as an argument and returns its amplitude.

Fundamentals

Stats:

CreatedSep 5, 2015
PublishedSep 5, 2015
Warriors Trained532
Total Skips22
Total Code Submissions770
Total Times Completed189
Python Completions189
Total Stars19
% of votes with a positive feedback rating96% of 69
Total "Very Satisfied" Votes65
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes1
Total Rank Assessments14
Average Assessed Rank
5 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • radogost Avatar
  • Blind4Basics Avatar
  • trashy_incel Avatar
Ad