#!/usr/bin/env python # coding: utf-8 # This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). # # Challenge Notebook # ## Problem: Invert a binary tree. # # * [Constraints](#Constraints) # * [Test Cases](#Test-Cases) # * [Algorithm](#Algorithm) # * [Code](#Code) # * [Unit Test](#Unit-Test) # * [Solution Notebook](#Solution-Notebook) # ## Constraints # # * What does it mean to invert a binary tree? # * Swap all left and right node pairs # * Can we assume we already have a Node class? # * Yes # * Can we assume the inputs are valid? # * No # * Can we assume this fits memory? # * Yes # ## Test Cases # #
# Input:
#      5
#    /   \
#   2     7
#  / \   / \
# 1   3 6   9
# 
# Output:
#      5
#    /   \
#   7     2
#  / \   / \
# 9   6 3   1
# 
# ## Algorithm # # Refer to the [Solution Notebook](http://nbviewer.jupyter.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/invert_tree/invert_tree_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start. # ## Code # In[ ]: get_ipython().run_line_magic('run', '../bst/bst.py') # In[ ]: class InverseBst(Bst): def invert_tree(self): # TODO: Implement me pass # ## Unit Test # **The following unit test is expected to fail until you solve the challenge.** # In[ ]: # %load test_invert_tree.py import unittest class TestInvertTree(unittest.TestCase): def test_invert_tree(self): root = Node(5) bst = InverseBst(root) node2 = bst.insert(2) node3 = bst.insert(3) node1 = bst.insert(1) node7 = bst.insert(7) node6 = bst.insert(6) node9 = bst.insert(9) result = bst.invert_tree() self.assertEqual(result, root) self.assertEqual(result.left, node7) self.assertEqual(result.right, node2) self.assertEqual(result.left.left, node9) self.assertEqual(result.left.right, node6) self.assertEqual(result.right.left, node3) self.assertEqual(result.right.right, node1) print('Success: test_invert_tree') def main(): test = TestInvertTree() test.test_invert_tree() if __name__ == '__main__': main() # ## Solution Notebook # # Review the [Solution Notebook]() for a discussion on algorithms and code solutions.