Question
In this question you will write a function that will return the depth of a given binary tree, whose root node is given as argument.
In this question you will write a function that will return the depth of a given binary tree, whose root node is given as argument.
- Implement tree_depth_r( root ) that strictly uses recursion to accomplish the task
This functions should return 0 if there is only a single (root) node in the provided binary tree.
You are to write these functions from scratch. This means your code should contain no import statements!
W A R N I N G
If you write this function disregarding the strict specifications mentioned above, you will not get any credit for your solutions, even if they happen to "work."
YOU MUST WRITE YOUR CODE IN THE FOLLOWING CODE CELL
In [ ]:
def tree_depth_r( root, depth=0 ):
"""
A recursive function that returns the depth of the provided binary tree
as an integer. The root of a binary tree is at depth 0.
This particular implementation uses a default argument depth=0. The important issue is that
this function can be called with a single argumen that represents the root of the binary tree
in question. In other words, as long as your function takes a single mandatory root argument,
you can write your function any way you wish as long as it is RECURSIVE! See the tests.
"""
# YOUR CODE HERE
pass
In [ ]:
In [ ]:
TESTS
In [ ]:
#
# T E S T #3.2.1
#
#
from tree_depth_r import tree_depth_r
student_answer = tree_depth_r( A )
correct_answer = 6
assert student_answer == correct_answer
print( ">>> TEST PASSED <<<" )
In [ ]:
#
# T E S T #3.2.2
#
#
from tree_depth_r import tree_depth_r
student_answer = tree_depth_r( S )
correct_answer = 7
assert student_answer == correct_answer
print( ">>> TEST PASSED <<<" )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started