def check(r, exc): a = init_tree(r) res = Solution().isBalanced(a) print(res) assert res == exc
输入: [1,null,0,0,1] 输出: [1,null,0,null,1] 解释: 只有红色节点满足条件“所有不包含 1 的子树”。 右图为返回的答案。 ? 这是中等?还是有更优的? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-pruning 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 """ from notebook.algorithm.树.utils import TreeNode from notebook.algorithm.树.utils import init_tree class Solution: def pruneTree(self, root: TreeNode) -> TreeNode: if not root: return None root.left = self.pruneTree(root.left) root.right = self.pruneTree(root.right) if not root.left and not root.right and root.val == 0: return None return root a = init_tree([1, 1, 0, 1, 1, 0, 1, 0]) a = Solution().pruneTree(a) print(a)
def check(r, exc): a = init_tree(r) res = Solution().countNodes(a) print(res) assert res == exc
from notebook.algorithm.树.utils import TreeNode from notebook.algorithm.树.utils import init_tree class Solution: def searchBST(self, root: TreeNode, val: int) -> TreeNode: if not root: return None if root.val == val: return root if root.val < val: return self.searchBST(root.right, val) return self.searchBST(root.left, val) a = init_tree([4, 2, 7, 1, 3]) res = Solution().searchBST(a, 2) print(res.val) """迭代""" class Solution: def searchBST(self, root: TreeNode, val: int) -> TreeNode: while root is not None and root.val != val: root = root.left if root.val > val else root.right return root print("迭代") a = init_tree([4, 2, 7, 1, 3]) res = Solution().searchBST(a, 2)
class Solution: def maxDepth(self, root: TreeNode) -> int: if not root: return 0 if not root.left and not root.right: return 1 cur_depth = 0 if root.left: cur_depth = self.maxDepth(root.left) + 1 if root.right: right_depth = self.maxDepth(root.right) + 1 cur_depth = right_depth if right_depth > cur_depth else cur_depth return cur_depth a = init_tree([3, 9, 20, None, None, 15, 7]) print(Solution().maxDepth(a)) """优雅很多""" class Solution: def maxDepth(self, root: TreeNode) -> int: if not root: return 0 return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 a = init_tree([3, 9, 20, None, None, 15, 7]) print(Solution().maxDepth(a)) """递归很多能通过栈实现
next_node.left = dnode.left else: # 否则替换节点为右子树的节点 next_node = dnode.right # 会是空 # 删除节点的父节点的子节点改为该最大节点 if pre: if opt == 1: pre.right = next_node else: pre.left = next_node else: root = next_node return root a = init_tree([5, 3, 6, 2, 4, None, 7]) res = Solution().deleteNode(a, 3) res = Solution().deleteNode(a, 0) a = init_tree([0]) res = Solution().deleteNode(a, 0) a = init_tree([5, 3, 6, 2, 4, None, 7]) res = Solution().deleteNode(a, 5) print(res.val) # f**k you """ [1,0,15,null,null,4,35,3,8,25,49,2,null,5,12,22,27,47,null,null,null,null,7,11,13,19,24,26,31,40,48,6,null,9,null,null,14,17,21,23,null,null,null,30,33,39,42,null,null,null,null,null,10,null,null,16,18,20,null,null,null,28,null,32,34,36,null,41,44,null,null,null,null,null,null,null,null,null,29,null,null,null,null,null,37,null,null,43,46,null,null,null,38,null,null,45] 22 输出: [1,0,15,null,null,4,35,3,8,25,49,2,null,5,12,21,27,47,null,null,null,null,7,11,13,19,24,26,31,40,48,6,null,9,null,null,14,17,null,23,null,null,null,30,33,39,42,null,null,null,null,null,10,null,null,16,18,null,null,28,null,32,34,36,null,41,44,null,null,null,null,null,null,null,29,null,null,null,null,null,37,null,null,43,46,null,null,null,38,null,null,45] 预期结果:
if not root.left and not root.right: return root.val, root.val left_min = rigth_max = root.val if root.left: left_min, left_max = self.isValidTree(root.left) if left_max is None or left_max >= root.val: return None, None if root.right: rigth_min, rigth_max = self.isValidTree(root.right) # rigth_min会是子节点的数字 if rigth_min is None or rigth_min <= root.val: return None, None return left_min, rigth_max a = init_tree([5, 1, 4, None, None, 3, 6]) print(Solution().isValidBST(a)) a = init_tree([2, 1, 3]) print(Solution().isValidBST(a)) a = init_tree([1, 1]) res = Solution().isValidBST(a) print(res) assert res == False """ 作者:LeetCode-Solution 链接:https://leetcode-cn.com/problems/validate-binary-search-tree/solution/yan-zheng-er-cha-sou-suo-shu-by-leetcode-solution/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 """