def buildTree_recur(preorder: List[int], inorder: List[int]) -> TreeNode: if (len(preorder) <= 0): return None #先序中的第一个元素是根节点 rootVal = preorder[0] idx = inorder.index(rootVal) # 构建左右子树 root = TreeNode(rootVal) root.left = buildTree_recur(preorder[1:idx + 1], inorder[0:idx]) #0省略 root.right = buildTree_recur(preorder[idx + 1:len(preorder)], inorder[idx + 1:len(inorder)]) #len省略 return root
def main(): root = TreeNode(1) n2 = TreeNode(2) n3 = TreeNode(3) root.setRightNode(n2) n2.setLeftNode(n3) ret = inorderTraversal(root) print(ret)
def mirrorTree(self, root: TreeNode) -> TreeNode: #递归交换左右节点 if not root: return root root.left, root.right = root.right, root.left if root.left: self.mirrorTree(root.left) if root.right: self.mirrorTree(root.right) return root
def sortedListToBST(head: ListNode) -> TreeNode: if head is None: return None if head.next is None: return TreeNode(head.val) fast = slow = prev = head while fast and fast.next: prev = slow slow = slow.next fast = fast.next.next # 根据中间节点一分为二 prev.next = None head2 = slow.next node = TreeNode(slow.val) node.left = sortedListToBST(head) node.right = sortedListToBST(head2) return node
def deserialize(self, data): """Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ if data == "[]": return vals, i = data[1:-1].split(','), 1 root = TreeNode(int(vals[0])) queue = collections.deque() queue.append(root) while queue: node = queue.popleft() if vals[i] != "null": node.left = TreeNode(int(vals[i])) queue.append(node.left) i += 1 if vals[i] != "null": node.right = TreeNode(int(vals[i])) queue.append(node.right) i += 1 return root
def build(preorder_left, preorder_right, inorder_left, inorder_right) -> TreeNode: if (preorder_left > preorder_right): return None # 前序遍历中的第一个节点就是根节点 preorder_root = preorder_left # 在中序遍历中定位根节点 inorder_root = index[preorder[preorder_root]] # 先把根节点建立出来 root = TreeNode(preorder[preorder_root]) # 得到左子树中的节点数目 size_left_subtree = inorder_root - inorder_left # 递归地构造左子树,并连接到根节点 # 先序遍历中「从 左边界+1 开始的 size_left_subtree」个元素就对应了中序遍历中「从 左边界 开始到 根节点定位-1」的元素 root.left = build(preorder_left + 1, preorder_left + size_left_subtree, inorder_left, inorder_root - 1) # 递归地构造右子树,并连接到根节点 # 先序遍历中「从 左边界+1+左子树节点数目 开始到 右边界」的元素就对应了中序遍历中「从 根节点定位+1 到 右边界」的元素 root.right = build(preorder_left + size_left_subtree + 1, preorder_right, inorder_root + 1, inorder_right) return root
def main(): # root = TreeNode(1) # n2 = TreeNode(2) # n3 = TreeNode(3) # root.setRightNode(n2) # n2.setLeftNode(n3) root = TreeNode(3) n2 = TreeNode(9) n3 = TreeNode(20) n4 = TreeNode(15) n5 = TreeNode(7) n6 = TreeNode(91) n7 = TreeNode(92) root.setLeftNode(n2) root.setRightNode(n3) n3.setLeftNode(n4) n3.setRightNode(n5) n2.setLeftNode(n6) n2.setRightNode(n7) """ 3 / \ 9 20 / \ / \ 91 92 15 7 """ ret = preorderTraversal(root) print(ret)
def main(): root = TreeNode(3) rLeft = TreeNode(4) rRight = TreeNode(5) root.setLeftNode(rLeft) root.setRightNode(rRight) rLeft.setLeftNode(TreeNode(1)) rLeft.setRightNode(TreeNode(2)) root2 = TreeNode(4) root2.setLeftNode(TreeNode(1)) solution = Solution() ret = solution.isSubStructure(root, root2) print(ret)
def main(): root = TreeNode(3) n2 = TreeNode(5) n3 = TreeNode(1) root.setLeftNode(n2) root.setRightNode(n3) n4 = TreeNode(6) n5 = TreeNode(2) n2.setLeftNode(n4) n2.setRightNode(n5) n6 = TreeNode(0) n7 = TreeNode(8) n3.setLeftNode(n6) n3.setRightNode(n7) solution = Solution() ret = solution.lowestCommonAncestor(root, n2, n3) print(ret.val)
def main(): root = TreeNode(1) rLeft = TreeNode(2) rRight = TreeNode(3) root.setLeftNode(rLeft) root.setRightNode(rRight) rLeft.setLeftNode(TreeNode(4)) rRight.setRightNode(TreeNode(5)) print('-level-') level_order_travel(root) solution = Solution() ret = solution.levelOrder(root) print(ret) ret = solution.levelOrder1(root) print(ret)
def main(): root = TreeNode(3) rLeft = TreeNode(4) rRight = TreeNode(5) root.setLeftNode(rLeft) root.setRightNode(rRight) rLeft.setLeftNode(TreeNode(1)) rLeft.setRightNode(TreeNode(2)) print('-root-') pre_order_travel(root) solution = Solution() ret = solution.mirrorTree(root) print('-pre-') pre_order_travel(ret) print('-in-') in_order_travel(ret) print('-post-') post_order_travel(ret)
def main(): root = TreeNode(3) rLeft = TreeNode(4) rRight = TreeNode(5) root.setLeftNode(rLeft) root.setRightNode(rRight) rLeft.setLeftNode(TreeNode(1)) rLeft.setRightNode(TreeNode(2)) solution = Solution() ret = solution.isSymmetric(root) print(ret)
def main(): root = TreeNode(3) n2 = TreeNode(9) n3 = TreeNode(20) n4 = TreeNode(15) n5 = TreeNode(7) root.setLeftNode(n2) root.setRightNode(n3) n3.setLeftNode(n4) n3.setRightNode(n5) solution = Solution() ret = solution.maxDepth2(root) print(ret)
def main(): root = TreeNode(3) n2 = TreeNode(2) n3 = TreeNode(3) n4 = TreeNode(3) n5 = TreeNode(1) root.setLeftNode(n2) root.setRightNode(n3) n2.setRightNode(n4) n3.setRightNode(n5) solution = Solution() ret = solution.rob(root) print(ret)