def tree(preorder_l: int, preorder_r: int, inorder_l: int, inorder_r: int) -> TreeNode: node = TreeNode.build(preorder[preorder_l], None, None) inorder_root_index = inorder_table[node.val] inorder_left_range = [inorder_l, inorder_root_index - 1] inorder_right_range = [inorder_root_index + 1, inorder_r] if inorder_left_range[-1] - inorder_left_range[0] >= 0: # 有左子树 preorder_left_range = [preorder_l + 1, preorder_r] for index in range(preorder_l + 1, preorder_r + 1): if inorder_table[preorder[index]] > inorder_left_range[-1]: preorder_left_range[-1] = index - 1 break node.left = tree(preorder_left_range[0], preorder_left_range[-1], inorder_left_range[0], inorder_left_range[-1]) if inorder_right_range[-1] - inorder_right_range[0] >= 0: # 有右子树 preorder_right_range = [preorder_l + 1, preorder_r] for index in range(preorder_r, preorder_l, -1): if inorder_table[preorder[index]] < inorder_right_range[0]: preorder_right_range[0] = index + 1 break node.right = tree(preorder_right_range[0], preorder_right_range[-1], inorder_right_range[0], inorder_right_range[-1]) return node
def sortedArrayToBST(self, nums: List[int]) -> TreeNode: if len(nums) == 0: return None midIndex = len(nums) // 2 midVal = nums[midIndex] leftVals = nums[:midIndex] rightVals = nums[midIndex + 1:] return TreeNode(midVal, left=self.sortedArrayToBST(leftVals), right=self.sortedArrayToBST(rightVals))
def sortedArrToBST(arr: []) -> TreeNode: if len(arr) == 0: return None midIndex = len(arr) // 2 midVal = arr[midIndex] leftVals = arr[:midIndex] rightVals = arr[midIndex + 1:] return TreeNode(midVal, left=sortedArrToBST(leftVals), right=sortedArrToBST(rightVals))
# self.val = x # self.left = None # self.right = None class Solution: def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]: ans = [] def nextNode(path: [int], node: TreeNode, pre_sum: int): if len(path) == 0 and node == None: return cur_sum = pre_sum + node.val cur_path = path + [node.val] if not node.left and not node.right and cur_sum == sum: ans.append(cur_path) else: if node.left: nextNode(cur_path, node.left, cur_sum) if node.right: nextNode(cur_path, node.right, cur_sum) nextNode([], root, 0) return ans # @lc code=end root = TreeNode.build( [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, None, 5, 1]) print(Solution().pathSum(root, 22))
# self.val = x # self.left = None # self.right = None class Solution: def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]: nodes, level = [root] if root else [], 0 ans = [] while len(nodes) > 0: _nodes = [] ans.append([]) if level % 2 == 0: for node in nodes: ans[-1].append(node.val) else: for node in reversed(nodes): ans[-1].append(node.val) for node in nodes: if node.left: _nodes.append(node.left) if node.right: _nodes.append(node.right) nodes = _nodes level += 1 return ans # @lc code=end tree = TreeNode.build([3,9,20,None,None,15,7]) print(Solution().zigzagLevelOrder(tree))
# @lc code=start # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isValidBST(self, root: TreeNode) -> bool: stack = [(root, None, None)] while len(stack) != 0: (node, minVal, maxVal) = stack.pop() if minVal is not None and node.val <= minVal: return False if maxVal is not None and node.val >= maxVal: return False if node.right is not None: stack.append((node.right, node.val, maxVal)) if node.left is not None: stack.append((node.left, minVal, node.val)) return True # @lc code=end print(Solution().isValidBST(TreeNode.build([0, None, -1]))) # print(Solution().isValidBST(TreeNode.build([2, 1, 3]))) # print(Solution().isValidBST(TreeNode.build([5, 1, 4, None, None, 3, 6]))) # print(Solution().isValidBST(TreeNode.build([5, 4, 6, None, None, 3, 7])))
if (node_p.right and node_q.right) or (node_p.right == None and node_q.right == None): if node_p.right and node_q.right: if node_p.right.val == node_q.right.val: stack_p.append(node_p.right) stack_q.append(node_q.right) else: return False else: return False if (node_p.left and node_q.left) or (node_p.left == None and node_q.left == None): if node_p.left and node_q.left: if node_p.left.val == node_q.left.val: stack_p.append(node_p.left) stack_q.append(node_q.left) else: return False else: return False return len(stack_p) == 0 and len(stack_q) == 0 # @lc code=end p = TreeNode.build([1, 2, 3]) q = TreeNode.build([1, 2, 3, 4, 5, 6]) print(Solution().isSameTree(p, q))
from Tool.Python.TreeNode import TreeNode # @lc code=start # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def rightSideView(self, root: TreeNode) -> List[int]: ans = [] levelNode = [root] if root else [] while len(levelNode) > 0: rightVal = None _levelNode = [] for node in levelNode: rightVal = node.val if node.left: _levelNode.append(node.left) if node.right: _levelNode.append(node.right) ans.append(rightVal) levelNode = _levelNode return ans # @lc code=end tree = TreeNode.build([1, 2]) print(Solution().rightSideView(tree))
node = root while len(stack) > 0: node = stack.pop() ans.append(node.val) if node.left: stack.append(node.left) if node.right: stack.append(node.right) ans.reverse() return ans # 其他做法 # def postorderTraversal(self, root: TreeNode) -> List[int]: # ans = [] # stack = [] # node = root # while len(stack) > 0 or node != None: # if node != None: # ans.append(node.val) # stack.append(node) # node = node.right # else: # node = stack.pop() # node = node.left # ans.reverse() # return ans # @lc code=end print(Solution().postorderTraversal(TreeNode.build([1,None,2,None,None,3])))
if self.function1(A.left, B, B_head): return True if self.function1(A.right, B, B_head): return True return False else: if B == B_head: if self.function1(A.left, B, B_head): return True if self.function1(A.right, B, B_head): return True return False # nodeA = TreeNode.build([3,4,5,1,2]) # nodeB = TreeNode.build([4,1]) # print(Solution().isSubStructure(nodeA, nodeB)) # nodeA = TreeNode.build([1,2,3,None,5,6,7,8,9,10,11,12,13]) # nodeB = TreeNode.build([3,6,None,None,13]) # print(Solution().isSubStructure(nodeA, nodeB)) # nodeA = TreeNode.build([4,2,3,4,5,6,7,8,9]) # nodeB = TreeNode.build([4,8,9]) # print(Solution().isSubStructure(nodeA, nodeB)) nodeA = TreeNode.build([1,0,1,-4,-3]) nodeB = TreeNode.build([1,-4]) print(Solution().isSubStructure(nodeA, nodeB))
stack.append(root) return stack else: stack.append(root) if root.left != None: if findNode(root.left, _node, stack): return stack if root.right != None: if findNode(root.right, _node, stack): return stack stack.pop() p_stack = findNode(root, p, []) q_stack = findNode(root, q, []) index = 0 while True: if index >= len(p_stack): return p_stack[index - 1] elif index >= len(q_stack): return q_stack[index - 1] elif p_stack[index] == q_stack[index]: index = index + 1 else: return p_stack[index - 1] # @lc code=end root = TreeNode.build([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]) p = root.left q = root.left.right.right print(Solution().lowestCommonAncestor(root, p, q).val)
# @lc code=start # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # 递归 def hasPathSum(self, root: TreeNode, sum: int) -> bool: if not root: return False sum -= root.val if not root.left and not root.right and sum == 0: return True return self.hasPathSum(root.left, sum) or self.hasPathSum # @lc code=end # root = TreeNode.build([5,4,8,11,None,13,4,7,2,None,None,None,1]) # root = TreeNode.build([1,2,None,3,None,4,None,5]) root = TreeNode.build([1]) print(Solution().hasPathSum(root, 1)) # 5 # 4 8 # 11 13 4 # 7 2 1
import os sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) from Tool.Python.TreeNode import TreeNode # @lc code=start # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isBalanced(self, root: TreeNode) -> bool: def totalLevel(root) -> (int, bool): if root == None: return (0, True) left = totalLevel(root.left) right = totalLevel(root.right) if left[1] and right[1] and abs(left[0] - right[0]) <= 1: return (max(left[0], right[0]) + 1, True) return (0, False) res = totalLevel(root) return res[1] # @lc code=end root = TreeNode.build([1, 2, 2, 3, 3, None, None, 4, 4]) print(Solution().isBalanced(root))
while len(nodes) > 0: leaf_nodes = [] for node in nodes: leaf_nodes.append(node.left if node and node.left else None) leaf_nodes.append(node.right if node and node.right else None) left, right, ok_nodes_count = 0, len(leaf_nodes) - 1, 0 while left < right: if leaf_nodes[left]: ok_nodes_count += 1 if leaf_nodes[right]: ok_nodes_count += 1 if leaf_nodes[left] and leaf_nodes[right] and leaf_nodes[ left].val == leaf_nodes[right].val: left += 1 right -= 1 continue if not leaf_nodes[left] and not leaf_nodes[right]: left += 1 right -= 1 continue return False if ok_nodes_count > 0: nodes = leaf_nodes else: nodes = [] return True # @lc code=end print(Solution().isSymmetric(TreeNode.build([1, 2, 2, None, 3, None, 3])))