mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge pull request #1835 from Logenleedev/my-contribution
添加leetcode 226翻转二叉树python后序递归代码
This commit is contained in:
@ -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
|
||||
```
|
||||
|
||||
递归法:精简代码
|
||||
|
@ -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:
|
||||
|
@ -74,7 +74,7 @@ public:
|
||||
|
||||
大家此时应该陷入深思.....
|
||||
|
||||
**其实队列没有必要维护窗口里的所有元素,只需要维护有可能成为窗口里最大值的元素就可以了,同时保证队里的元素数值是由大到小的。**
|
||||
**其实队列没有必要维护窗口里的所有元素,只需要维护有可能成为窗口里最大值的元素就可以了,同时保证队列里的元素数值是由大到小的。**
|
||||
|
||||
那么这个维护元素单调递减的队列就叫做**单调队列,即单调递减或单调递增的队列。C++中没有直接支持单调队列,需要我们自己来实现一个单调队列**
|
||||
|
||||
|
Reference in New Issue
Block a user