Merge pull request #1835 from Logenleedev/my-contribution

添加leetcode 226翻转二叉树python后序递归代码
This commit is contained in:
程序员Carl
2023-01-06 10:18:27 +08:00
committed by GitHub
3 changed files with 17 additions and 5 deletions

View File

@ -402,10 +402,10 @@ class solution:
def getdepth(self, node): def getdepth(self, node):
if not node: if not node:
return 0 return 0
leftdepth = self.getdepth(node.left) #左 leftheight = self.getdepth(node.left) #左
rightdepth = self.getdepth(node.right) #右 rightheight = self.getdepth(node.right) #右
depth = 1 + max(leftdepth, rightdepth) #中 height = 1 + max(leftheight, rightheight) #中
return depth return height
``` ```
递归法:精简代码 递归法:精简代码

View File

@ -322,6 +322,18 @@ class Solution:
return root 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 ```python
class Solution: class Solution:

View File

@ -74,7 +74,7 @@ public:
大家此时应该陷入深思..... 大家此时应该陷入深思.....
**其实队列没有必要维护窗口里的所有元素,只需要维护有可能成为窗口里最大值的元素就可以了,同时保证队里的元素数值是由大到小的。** **其实队列没有必要维护窗口里的所有元素,只需要维护有可能成为窗口里最大值的元素就可以了,同时保证队里的元素数值是由大到小的。**
那么这个维护元素单调递减的队列就叫做**单调队列即单调递减或单调递增的队列。C++中没有直接支持单调队列,需要我们自己来实现一个单调队列** 那么这个维护元素单调递减的队列就叫做**单调队列即单调递减或单调递增的队列。C++中没有直接支持单调队列,需要我们自己来实现一个单调队列**