diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 03322c9a..e54221db 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -402,10 +402,10 @@ class solution: def getdepth(self, node): if not node: return 0 - leftdepth = self.getdepth(node.left) #左 - rightdepth = self.getdepth(node.right) #右 - depth = 1 + max(leftdepth, rightdepth) #中 - return depth + leftheight = self.getdepth(node.left) #左 + rightheight = self.getdepth(node.right) #右 + height = 1 + max(leftheight, rightheight) #中 + return height ``` 递归法:精简代码 diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index e55e1240..3982a450 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -322,6 +322,18 @@ class Solution: return root ``` +递归法:后序遍历: +```python +class Solution: + def invertTree(self, root: TreeNode) -> TreeNode: + if root is None: + return None + self.invertTree(root.left) + self.invertTree(root.right) + root.left, root.right = root.right, root.left + return root +``` + 迭代法:深度优先遍历(前序遍历): ```python class Solution: diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index 9f2e96a6..540ab5d7 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -74,7 +74,7 @@ public: 大家此时应该陷入深思..... -**其实队列没有必要维护窗口里的所有元素,只需要维护有可能成为窗口里最大值的元素就可以了,同时保证队里的元素数值是由大到小的。** +**其实队列没有必要维护窗口里的所有元素,只需要维护有可能成为窗口里最大值的元素就可以了,同时保证队列里的元素数值是由大到小的。** 那么这个维护元素单调递减的队列就叫做**单调队列,即单调递减或单调递增的队列。C++中没有直接支持单调队列,需要我们自己来实现一个单调队列**