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.
Similar Kata:
Stats:
Created | Sep 5, 2015 |
Published | Sep 5, 2015 |
Warriors Trained | 532 |
Total Skips | 22 |
Total Code Submissions | 770 |
Total Times Completed | 189 |
Python Completions | 189 |
Total Stars | 19 |
% of votes with a positive feedback rating | 96% of 69 |
Total "Very Satisfied" Votes | 65 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 14 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 8 kyu |