stack, ans = list(), list() i = 0 if A: stack.append(A) while stack: node = stack[-1] if not node.left or hasattr(node.left, 'visitedp'): if not node.right or hasattr(node.right, 'visitedp'): ans.append(stack.pop().val) node.visitedp = True else: stack.append(node.right) else: stack.append(node.left) return ans # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # if __name__ == '__main__': # Input DATA to practice: 11 2 9 13 57 25 17 1 90 3 r = TreeNode(int(sys.argv[1])) for arg in range(2, len(sys.argv), 1): insertNode(r, TreeNode(int(sys.argv[arg]))) s = Solution() L = s.postorderTraversal(r) print("Post Order Traversal: {}".format(L))
# def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): # @param A : root node of tree # @return an integer def maxDepth(self, A): if A is None: return 0 return max(self.maxDepth(A.left), self.maxDepth(A.right)) + 1 def minDepth(self, root): if not root: return 0 if not root.left: return self.minDepth(root.right) + 1 elif not root.right: return self.minDepth(root.left) + 1 else: return min(self.minDepth(root.left), self.minDepth(root.right)) + 1 if __name__ == '__main__': r = TreeNode(1) insertNode(r, TreeNode(2)) s = Solution() print(s.maxDepth(r)) print(s.minDepth(r))
if root is None: return 0 else: return 1 + max(getHight(root.left), getHight(root.right)) def isAvl(root): if root: h = abs(getHight(root.left) - getHight(root.right)) if h > 1: return False else: isAvl(root.left) isAvl(root.right) return True t = None t = insertNode(t, 5) t = insertNode(t, 3) t = insertNode(t, 10) t = insertNode(t, 2) t = insertNode(t, 4) t = insertNode(t, 0) t = insertNode(t, 1) t = insertNode(t, -1) #t=insertNode(t,-2) t = insertNode(t, 6) t = insertNode(t, 11) print(isAvl(t))
from BST import Node from BST import insertNode from BST import Display from BST import deleteNode t = None t = insertNode(t, 12) t = insertNode(t, 32) t = insertNode(t, 2) t = insertNode(t, 24) t = insertNode(t, 3) t = insertNode(t, 36) t = insertNode(t, 0) Display(t) t = deleteNode(t, 12) Display(t) t = deleteNode(t, 2) Display(t) t = deleteNode(t, 0) Display(t) t = deleteNode(t, 36) Display(t)