""" :type root: TreeNode :rtype: List[List[int]] """ if root is None: return [] current_level = [root] next_level = [] result = [] while current_level: level_value_list = [] for current_node in current_level: level_value_list.append(current_node.val) next_level.append( current_node.left) if current_node.left else None next_level.append( current_node.right) if current_node.right else None current_level = next_level next_level = [] result.append(level_value_list) return result if __name__ == '__main__': t = TreeNode.build_by_level_order([1, 2, 3, '#', '#', 4, '#', '#', 5]) print Solution().levelOrder(t) TreeNode.print_in_level_order(t) t2 = TreeNode.build_by_level_order([3, 9, 20, '#', '#', 15, 7]) print Solution().levelOrder(t2) t3 = TreeNode.build_by_level_order([3]) print Solution().levelOrder(t3)
class Solution(object): def sortedArrayToBST(self, nums): """ :type nums: List[int] :rtype: TreeNode """ def op(start, end): if start == end: return TreeNode(nums[start]) elif end - start == 1: root = TreeNode(nums[start]) root.right = TreeNode(nums[end]) return root else: pivot = (start + end)/2 root = TreeNode(nums[pivot]) root.left = op(start, pivot-1) root.right = op(pivot+1, end) return root if len(nums) == 0: return None return op(0, len(nums)-1) if __name__ == '__main__': TreeNode.print_in_level_order(Solution().sortedArrayToBST([1,2,3,4,5])) TreeNode.print_in_level_order(Solution().sortedArrayToBST([1,2,3,4,5,6])) TreeNode.print_in_level_order(Solution().sortedArrayToBST([1,2])) TreeNode.print_in_level_order(Solution().sortedArrayToBST([1])) TreeNode.print_in_level_order(Solution().sortedArrayToBST([]))
__author__ = 'clp' from utils import TreeNode class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ def invert_tree(root): if root is None: return None root.left, root.right = root.right, root.left invert_tree(root.left) invert_tree(root.right) if root: invert_tree(root) return root if __name__ == '__main__': TreeNode.print_in_level_order(Solution().invertTree(TreeNode.build_by_level_order([4, 2, 7, 1, '#',6,9]))) TreeNode.print_in_level_order(Solution().invertTree(TreeNode.build_by_level_order([])))
from utils import TreeNode, ListNode class Solution(object): def sortedListToBST(self, head): """ :type head: ListNode :rtype: TreeNode """ if head is None: return None fast, prev, slow = head, None, head while fast and fast.next: fast = fast.next.next prev, slow = slow, slow.next root = TreeNode(slow.val) if prev: prev.next = None else: return root root.left = self.sortedListToBST(head) root.right = self.sortedListToBST(slow.next) return root if __name__ == '__main__': TreeNode.print_in_level_order(Solution().sortedListToBST(ListNode.build_linked_list([1,2]))) TreeNode.print_in_level_order(Solution().sortedListToBST(ListNode.build_linked_list([1]))) TreeNode.print_in_level_order(Solution().sortedListToBST(ListNode.build_linked_list([1,2,3,4,5,6]))) TreeNode.print_in_level_order(Solution().sortedListToBST(ListNode.build_linked_list([1,2,3,4,5]))) TreeNode.print_in_level_order(Solution().sortedListToBST(ListNode.build_linked_list([])))
:type head: ListNode :rtype: TreeNode """ if head is None: return None fast, prev, slow = head, None, head while fast and fast.next: fast = fast.next.next prev, slow = slow, slow.next root = TreeNode(slow.val) if prev: prev.next = None else: return root root.left = self.sortedListToBST(head) root.right = self.sortedListToBST(slow.next) return root if __name__ == '__main__': TreeNode.print_in_level_order(Solution().sortedListToBST( ListNode.build_linked_list([1, 2]))) TreeNode.print_in_level_order(Solution().sortedListToBST( ListNode.build_linked_list([1]))) TreeNode.print_in_level_order(Solution().sortedListToBST( ListNode.build_linked_list([1, 2, 3, 4, 5, 6]))) TreeNode.print_in_level_order(Solution().sortedListToBST( ListNode.build_linked_list([1, 2, 3, 4, 5]))) TreeNode.print_in_level_order(Solution().sortedListToBST( ListNode.build_linked_list([])))
""" :type root: TreeNode :rtype: List[List[int]] """ if root is None: return [] current_level = [root] next_level = [] result = [] while current_level: level_value_list = [] for current_node in current_level: level_value_list.append(current_node.val) next_level.append(current_node.left) if current_node.left else None next_level.append(current_node.right) if current_node.right else None current_level = next_level next_level = [] result.append(level_value_list) return result if __name__ == '__main__': t = TreeNode.build_by_level_order([1,2,3,'#','#',4,'#','#',5]) print Solution().levelOrder(t) TreeNode.print_in_level_order(t) t2 = TreeNode.build_by_level_order([3,9,20,'#','#',15,7]) print Solution().levelOrder(t2) t3 = TreeNode.build_by_level_order([3]) print Solution().levelOrder(t3)
__author__ = 'clp' from utils import TreeNode class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ def invert_tree(root): if root is None: return None root.left, root.right = root.right, root.left invert_tree(root.left) invert_tree(root.right) if root: invert_tree(root) return root if __name__ == '__main__': TreeNode.print_in_level_order(Solution().invertTree( TreeNode.build_by_level_order([4, 2, 7, 1, '#', 6, 9]))) TreeNode.print_in_level_order(Solution().invertTree( TreeNode.build_by_level_order([])))
""" :type nums: List[int] :rtype: TreeNode """ def op(start, end): if start == end: return TreeNode(nums[start]) elif end - start == 1: root = TreeNode(nums[start]) root.right = TreeNode(nums[end]) return root else: pivot = (start + end) / 2 root = TreeNode(nums[pivot]) root.left = op(start, pivot - 1) root.right = op(pivot + 1, end) return root if len(nums) == 0: return None return op(0, len(nums) - 1) if __name__ == '__main__': TreeNode.print_in_level_order(Solution().sortedArrayToBST([1, 2, 3, 4, 5])) TreeNode.print_in_level_order(Solution().sortedArrayToBST( [1, 2, 3, 4, 5, 6])) TreeNode.print_in_level_order(Solution().sortedArrayToBST([1, 2])) TreeNode.print_in_level_order(Solution().sortedArrayToBST([1])) TreeNode.print_in_level_order(Solution().sortedArrayToBST([]))