if not root: return [] # 先正向层序遍历 result_forward = [] node_current_layer, node_next_layer = deque(), deque() node_current_layer.append(root) node_current_data = [] while node_current_layer or node_next_layer: node = node_current_layer.popleft() node_current_data.append(node.val) if node.left: node_next_layer.append(node.left) if node.right: node_next_layer.append(node.right) if not node_current_layer: node_current_layer = node_next_layer node_next_layer = deque() result_forward.append(node_current_data) node_current_data = [] return result_forward[::-1] if __name__ == '__main__': tree = buildTree([3, 9, 20, 'null', 'null', 15, 7]) print(Solution().levelOrderBottom(tree))
# self.right = None class Solution: def findFatherRecur(self, root, p, q): if (root.val < p.val and root.val > q.val or root.val > p.val and root.val < q.val): return root if root.val == p.val or root.val == q.val: return root if root.val < p.val and root.val < q.val: return self.findFatherRecur(root.right, p, q) else: return self.findFatherRecur(root.left, p, q) def lowestCommonAncestor(self, root, p, q): """ :type root: TreeNode :type p: TreeNode :type q: TreeNode :rtype: TreeNode """ return self.findFatherRecur(root, p, q) if __name__ == '__main__': tree = buildTree([6, 2, 8, 0, 4, 7, 9, 'null', 'null', 3, 5]) p, q = TreeNode(2), TreeNode(8) print(Solution().lowestCommonAncestor(tree, p, q))
self.sumNumbersRecur(root.right, cur_num) else: self._path_sum += cur_num def sumNumbers(self, root): """ :type root: TreeNode :rtype: int """ if not root: return 0 cur_num = root.val if root.left and root.right: self.sumNumbersRecur(root.left, cur_num) self.sumNumbersRecur(root.right, cur_num) elif root.left: self.sumNumbersRecur(root.left, cur_num) elif root.right: self.sumNumbersRecur(root.right, cur_num) else: self._path_sum += cur_num return self._path_sum if __name__ == '__main__': tree = buildTree([1, 2, 3]) print(Solution().sumNumbers(tree))
if not root: return self.getMinimumDifferenceRecur(root.left, data) data.append(root.val) self.getMinimumDifferenceRecur(root.right, data) def getMinimumDifference(self, root): """ :type root: TreeNode :rtype: int """ data = [] self.getMinimumDifferenceRecur(root, data) min_diff = float('inf') for i in range(1, len(data)): if data[i] - data[i - 1] < min_diff: min_diff = data[i] - data[i - 1] return min_diff if __name__ == '__main__': nums = [1, 'null', 3, 2] root = buildTree(nums) print(Solution().getMinimumDifference(root))
:type root: TreeNode :rtype: bool """ last_layer_flag = False cur_layer = [root] while cur_layer: next_layer = [] for node in cur_layer: if not node: last_layer_flag = True continue if last_layer_flag: return False next_layer.append(node.left) next_layer.append(node.right) cur_layer = next_layer return True if __name__ == '__main__': tree = buildTree([1,2,3,4,5,6]) print(Solution().isCompleteTree(tree))
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if not root: return None root.right, root.left = root.left, root.right if root and root.left: self.invertTree(root.left) if root and root.right: self.invertTree(root.right) return root if __name__ == '__main__': tree = buildTree([4, 2, 7, 1, 3, 6, 9]) tree_reverse = Solution().invertTree(tree) level_order_traversal(tree_reverse)
from Tree_Method import TreeNode, buildTree class Solution(object): def tree2str(self, t): """ :type t: TreeNode :rtype: str """ if not t: return "" s = str(t.val) if t.left or t.right: s += "(" + self.tree2str(t.left) + ")" if t.right: s += "(" + self.tree2str(t.right) + ")" return s if __name__ == '__main__': tree = buildTree([1,2,3,'null',4]) print(Solution().tree2str(tree))
from collections import deque from Tree_Method import TreeNode,buildTree class Solution: def dfs(self, node, i, depth, leftmosts): if not node: return 0 if depth >= len(leftmosts): leftmosts.append(i) return max(i - leftmosts[depth] + 1, self.dfs(node.left, i * 2, depth + 1, leftmosts),self.dfs(node.right, i * 2 + 1, depth + 1, leftmosts)) def widthOfBinaryTree(self, root): """ :type root: TreeNode :rtype: int """ leftmosts = [] return self.dfs(root, 1, 0, leftmosts) if __name__ == '__main__': # tree = buildTree([1,1,1,1,'null','null',1,1,'null','null','null','null','null','null',1]) tree = buildTree([1,3,2,5,3,'null',9]) print(Solution().widthOfBinaryTree(tree))
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def flipEquivReccur(self, root1, root2): if not root1 and not root2: return True if (not root1 and root2) or (root1 and not root2): return False if root1.val != root2.val: return False return (self.flipEquivReccur(root1.left, root2.left) and self.flipEquivReccur(root1.right, root2.right)) or \ (self.flipEquivReccur(root1.left, root2.right) and self.flipEquivReccur(root1.right, root2.left)) def flipEquiv(self, root1, root2): """ :type root1: TreeNode :type root2: TreeNode :rtype: bool """ return self.flipEquivReccur(root1, root2) if __name__ == '__main__': tree1 = buildTree([1,2,3,4,5,6,'null','null','null',7,8]) tree2 = buildTree([1,3,2,'null',6,4,5,'null','null','null','null',8,7]) print(Solution().flipEquiv(tree1, tree2))
if root.left: path.append(root.left) self.binaryTreePathsRec(root.left, path, result) path.pop() if root.right: path.append(root.right) self.binaryTreePathsRec(root.right, path, result) path.pop() def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ if not root: return [] result = [] path = [] path.append(root) self.binaryTreePathsRec(root, path, result) return result if __name__ == '__main__': tree = buildTree([1, 2, 3, "null", 5]) print(Solution().binaryTreePaths(tree))
return False else: if not root2.left and not root2.right: return True else: return False def flipEquiv(self, root1, root2): """ :type root1: TreeNode :type root2: TreeNode :rtype: bool """ if not root1 and not root2: return True elif root1 and not root2: return False elif root2 and not root1: return False elif root1.val == root2.val: return self.flipEquivRecur(root1, root2) else: return False if __name__ == '__main__': tree1 = buildTree([0, 2, 1, 'null', 3, 'null', 5, 'null', 4]) tree2 = buildTree([0, 'null', 2, 3, 'null', 'null', 4]) # level_order_traversal(tree1) # level_order_traversal(tree2) print(Solution().flipEquiv(tree1, tree2))