__author__ = 'Connor' from TreeNode import consturctdefault from TreeNode import printTree # @param root, a tree node # @return a list of integers def inorderTraversal(root): p = root ans = [] stack = [] while p != None or stack != []: while p != None: stack.append(p) p = p.left if stack != []: p = stack.pop() ans.append(p.val) p = p.right return ans tt = consturctdefault() printTree(tt) ans = inorderTraversal(tt) print(ans)
elif sp == -1: return True else: if sp == 1: return p.val == q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right) elif sp == 2: return p.val == q.val and self.isSameTree(p.right,q.right) elif sp == 3: return p.val == q.val and self.isSameTree(p.left,q.left) elif sp == 0: return p.val == q.val def NodeStructure(p): if p == None: return -1 if p.left != None and p.right != None: return 1 elif p.left == None and p.right != None: return 2 elif p.left!= None and p.right == None: return 3 elif p.left == None and p.right == None: return 0 if __name__ == '__main__': t1 = consturctdefault() t2 = consturctdefault() t3 = constructBST() so = Solution() ans = so.isSameTree(t1,t3) print(ans)
__author__ = 'Connor' from TreeNode import consturctdefault from TreeNode import constructBST from TreeNode import constructSymmeric class Solution: # @param root, a tree node # @return an integer def maxDepth(self, root): if root == None: return 0 elif root.left == None and root.right == None: return 1 else: return 1 + max(self.maxDepth(root.left),self.maxDepth(root.right)) if __name__ == '__main__': tt = consturctdefault() tb = constructBST() ts = constructSymmeric() so = Solution() print(so.maxDepth(tt)) print(so.maxDepth(tb)) print(so.maxDepth(ts))
if sp == 1: return p.val == q.val and self.isSameTree( p.left, q.left) and self.isSameTree(p.right, q.right) elif sp == 2: return p.val == q.val and self.isSameTree(p.right, q.right) elif sp == 3: return p.val == q.val and self.isSameTree(p.left, q.left) elif sp == 0: return p.val == q.val def NodeStructure(p): if p == None: return -1 if p.left != None and p.right != None: return 1 elif p.left == None and p.right != None: return 2 elif p.left != None and p.right == None: return 3 elif p.left == None and p.right == None: return 0 if __name__ == '__main__': t1 = consturctdefault() t2 = consturctdefault() t3 = constructBST() so = Solution() ans = so.isSameTree(t1, t3) print(ans)