摘要: 问题描述:一条纸条,对折N次,折痕朝向有凹有凸,从左到右依次输出折痕 解题思路:假设对折一次的折痕记为“凹”,对折N次会发现,折痕会形成左子树都为“凹”,右子树都为“凸”的二叉树,最终遍历的结果,为折痕形成树形结构的中序遍历结果。 1 def print_all_folds(n): 2 print_ 阅读全文
posted @ 2022-01-15 21:23 CaptainDragonfly 阅读(63) 评论(0) 推荐(0) 编辑
摘要: 原题链接:https://leetcode-cn.com/problems/serialize-and-deserialize-bst/ 相同的题: https://leetcode-cn.com/problems/h54YBf/ https://leetcode-cn.com/problems/x 阅读全文
posted @ 2022-01-15 20:44 CaptainDragonfly 阅读(34) 评论(0) 推荐(0) 编辑
摘要: 1 class TreeNode: 2 def __init__(self, val=0, left=None, right=None, parent=None): 3 self.val = val 4 self.left = left 5 self.right = right 6 self.par 阅读全文
posted @ 2022-01-15 20:19 CaptainDragonfly 阅读(65) 评论(0) 推荐(0) 编辑
摘要: 原题链接:https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others/ 代码 1 class Solution: 2 def dominantIndex(self, nums: List[int]) -> int: 阅读全文
posted @ 2022-01-13 23:54 CaptainDragonfly 阅读(19) 评论(0) 推荐(0) 编辑
摘要: 原题链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/ 相同题: LeetCode235:https://leetcode-cn.com/problems/lowest-common-ancesto 阅读全文
posted @ 2022-01-13 23:33 CaptainDragonfly 阅读(22) 评论(0) 推荐(0) 编辑
摘要: 原题链接:https://leetcode-cn.com/problems/water-bottles/ 代码: 1 class Solution: 2 def numWaterBottles(self, numBottles: int, numExchange: int) -> int: 3 to 阅读全文
posted @ 2022-01-13 21:48 CaptainDragonfly 阅读(16) 评论(0) 推荐(0) 编辑
摘要: 原题链接:https://leetcode-cn.com/problems/count-complete-tree-nodes/ 解题思路:树形DP 代码: 1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __in 阅读全文
posted @ 2022-01-13 21:46 CaptainDragonfly 阅读(19) 评论(0) 推荐(0) 编辑
摘要: 原题链接:https://leetcode-cn.com/problems/balanced-binary-tree/,https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/ 解题思路:递归。左右子树的高度差不大于1,且左右子树都是平衡 阅读全文
posted @ 2022-01-12 23:19 CaptainDragonfly 阅读(17) 评论(0) 推荐(0) 编辑
摘要: 1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = le 阅读全文
posted @ 2022-01-12 20:41 CaptainDragonfly 阅读(31) 评论(0) 推荐(0) 编辑
摘要: 原题链接:https://leetcode-cn.com/problems/two-sum/submissions/ 解题思路:暴力法 代码: 1 class Solution: 2 # 暴力法 3 def twoSum(self, nums: List[int], target: int) -> 阅读全文
posted @ 2022-01-11 23:01 CaptainDragonfly 阅读(17) 评论(0) 推荐(0) 编辑