| 
 | 1 | +class Solution:  | 
 | 2 | +    def __init__(self,x):  | 
 | 3 | +        self.val = x  | 
 | 4 | +        self.left = None  | 
 | 5 | +        self.right= None  | 
 | 6 | +    def postorderTraversal(self,root):  | 
 | 7 | +        result = []  | 
 | 8 | +        self.recur(root,result)  | 
 | 9 | +        return result  | 
 | 10 | +    def recur(self,root,result):  | 
 | 11 | +        if (root ==None):  | 
 | 12 | +            return   | 
 | 13 | +        self.recur(root.left,result)  | 
 | 14 | +        self.recur(root.right,result)  | 
 | 15 | +        print root.val  | 
 | 16 | +        result.append(root.val)  | 
 | 17 | +      | 
 | 18 | +    def preorderTraversal(self,root):  | 
 | 19 | +        result = []  | 
 | 20 | +        print 'pre order'  | 
 | 21 | +        self.recurPre(root,result)  | 
 | 22 | +        return result  | 
 | 23 | +    def recurPre(self,root,result):  | 
 | 24 | +        if (root ==None):  | 
 | 25 | +            return  | 
 | 26 | +        result.append(root.val)  | 
 | 27 | +        print root.val  | 
 | 28 | +        self.recurPre(root.left,result)  | 
 | 29 | +        self.recurPre(root.right,result)  | 
 | 30 | +          | 
 | 31 | +          | 
 | 32 | +    def inorderTraversal(self,root):  | 
 | 33 | +        result = []  | 
 | 34 | +        print 'In order traversal'  | 
 | 35 | +        self.recurInorder(root,result)  | 
 | 36 | +        return result  | 
 | 37 | +    def recurInorder(self,root,result):  | 
 | 38 | +        if (root==None):  | 
 | 39 | +            return   | 
 | 40 | +        self.recurInorder(root.left,result)  | 
 | 41 | +        print root.val  | 
 | 42 | +        result.append(root.val)  | 
 | 43 | +        self.recurInorder(root.right,result)  | 
 | 44 | +          | 
 | 45 | +          | 
 | 46 | +a = Solution(1)  | 
 | 47 | + | 
 | 48 | +b = Solution(2)  | 
 | 49 | +c = Solution(3)  | 
 | 50 | +d = Solution(4)  | 
 | 51 | +e = Solution(5)  | 
 | 52 | + | 
 | 53 | +a.left = b  | 
 | 54 | +a.right = d  | 
 | 55 | +b.left = c  | 
 | 56 | +d.right = e  | 
 | 57 | +print a.postorderTraversal(a)  | 
 | 58 | +print a.preorderTraversal(a)  | 
 | 59 | +print a.inorderTraversal(a)  | 
0 commit comments