blob: ab6a1908678e7322569d38c67c0c9e655a7e4b00 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
│ You are given the root of a binary tree. Invert the binary tree in
│ place. That is, all left children should become right children, and all
│ right children should become left children.
│ Example:
│ a
│ / \
│ b c
│ / \ /
│d e f
│
│ The inverted version of this tree is as follows:
│ a
│ / \
│ c b
│ \ / \
│ f e d
│
│ Here is the function signature:
│class Node:
│ def __init__(self, value):
│ self.left = None
│ self.right = None
│ self.value = value
│ def preorder(self):
│ print self.value,
│ if self.left: self.left.preorder()
│ if self.right: self.right.preorder()
│def invert(node):
│ # Fill this in.
│root = Node('a')
│root.left = Node('b')
│root.right = Node('c')
│root.left.left = Node('d')
│root.left.right = Node('e')
│root.right.left = Node('f')
│root.preorder()
│# a b d e c f
│print "\n"
│invert(root)
│root.preorder()
│# a c f b e d
|