__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)
def isBalanced(self, root): if root == None: return True hl = height(root.left) hr = height(root.right) return abs(hl - hr) <= 1 and self.isBalanced( root.left) and self.isBalanced(root.right) def height(root): if root == None: return 0 else: if root.left == None and root.right == None: return 1 elif root.left == None and root.right != None: return 1 + height(root.right) elif root.left != None and root.right == None: return 1 + height(root.left) else: return max(height(root.left), height(root.right)) + 1 if __name__ == '__main__': so = Solution() # tt = constructdefault1() tb = constructUnbalanced() printTree(tb) # print(so.isBalanced(tt)) print(so.isBalanced(tb))
return True return isSubTreeLess(root.left,root.val) and isSubTreeGreater(root.right,root.val) and isValidBST(root.left) and isValidBST(root.right) def isSubTreeLess(root,val): if root == None: return True return root.val<val and isSubTreeLess(root.left,val) and isSubTreeLess(root.right,val) def isSubTreeGreater(root,val): if root == None: return True return root.val>val and isSubTreeLess(root.left,val) and isSubTreeGreater(root.right,val) def isValidBST2(root): if root == None: return True return rec(root,-sys.maxsize-1,sys.maxsize) def rec(root,min,max): if root == None: return True if root.val<=min or root.val>=max: return False return rec(root.left,min,root.val) and rec(root.right,root.val,max) tt = constructBST() printTree(tt) # global previous previous = tt.val print(isValidBST2(tt))
# if ltrees != [None]: # for lt in ltrees: # root.left = lt # if rtrees != [None]: # for rt in rtrees: # root.right = rt # ans.append(root) # else: # root.right = None # ans.append(root) # else: # root.left = None # if rtrees != [None]: # for rt in rtrees: # root.right = rt # ans.append(root) # else: # root.right = None # ans.append(root) return ans if __name__ == '__main__': so = Solution() ans = so.generateTrees(3) cnt = 0 print(ans) for i in ans: cnt += 1 print('Num'+str(cnt)) printTree(i)
__author__ = 'Connor' from TreeNode import TreeNode from TreeNode import printTree class Solution: # @param inorder, a list of integers # @param postorder, a list of integers # @return a tree node def buildTree(self, inorder, postorder): if inorder == []: return None root = TreeNode(postorder[len(postorder)-1]) length = inorder.index(postorder[len(postorder)-1]) if length != len(postorder)-1: root.right = self.buildTree(inorder[length+1:],postorder[length: len(postorder)-1]) if length > 0: root.left = self.buildTree(inorder[:length],postorder[:length]) return root if __name__ == '__main__': postOrder = [7,4,2,5,8,6,3,1] inOrder = [4,7,2,1,5,3,8,6] so = Solution() ans = so.buildTree(inOrder,postOrder) printTree(ans)
__author__ = 'Connor' from TreeNode import TreeNode from TreeNode import printTree class Solution: # @param preorder, a list of integers # @param inorder, a list of integers # @return a tree node def buildTree(self, preorder, inorder): if preorder == []: return None root = TreeNode(preorder[0]) length = inorder.index(preorder[0]) if length != 0: root.left = self.buildTree(preorder[1:length+1],inorder[:length]) if len(preorder) > length: root.right = self.buildTree(preorder[length+1:],inorder[length+1:]) return root if __name__ == '__main__': preOrder = [1,2,4,7,3,5,6,8] inOrder = [4,7,2,1,5,3,8,6] so = Solution() ans = so.buildTree(preOrder,inOrder) print(ans) printTree(ans)
def isBalanced(self, root): if root == None: return True hl = height(root.left) hr = height(root.right) return abs(hl -hr) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right) def height(root): if root == None: return 0 else: if root.left == None and root.right == None: return 1 elif root.left == None and root.right != None: return 1 + height(root.right) elif root.left != None and root.right == None: return 1 + height(root.left) else: return max(height(root.left),height(root.right))+1 if __name__ == '__main__': so = Solution() # tt = constructdefault1() tb = constructUnbalanced() printTree(tb) # print(so.isBalanced(tt)) print(so.isBalanced(tb))
else: tmp = root.right self.flatten(tmp) root.right = root.left root.left = None self.flatten(root.right) p = root.right while p.right != None: p = p.right p.right = tmp return # p = root # tmp = p.right # p.right = p.left # self.flatten(p.left) # q = p.left # while q.right != None: # q = q.right # q.right = tmp # return def isLeaf(root): return root.left == None and root.right == None if __name__ == '__main__': so = Solution() tf = contstructFlatten() so.flatten(tf) printTree(tf)
# for lt in ltrees: # root.left = lt # if rtrees != [None]: # for rt in rtrees: # root.right = rt # ans.append(root) # else: # root.right = None # ans.append(root) # else: # root.left = None # if rtrees != [None]: # for rt in rtrees: # root.right = rt # ans.append(root) # else: # root.right = None # ans.append(root) return ans if __name__ == '__main__': so = Solution() ans = so.generateTrees(3) cnt = 0 print(ans) for i in ans: cnt += 1 print('Num' + str(cnt)) printTree(i)