mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge pull request #323 from z80160280/master
添加 0226 0101 0104 python版本
This commit is contained in:
@ -360,6 +360,78 @@ Java:
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
> 递归法
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def isSymmetric(self, root: TreeNode) -> bool:
|
||||||
|
if not root:
|
||||||
|
return True
|
||||||
|
return self.compare(root.left, root.right)
|
||||||
|
|
||||||
|
def compare(self, left, right):
|
||||||
|
#首先排除空节点的情况
|
||||||
|
if left == None and right != None: return False
|
||||||
|
elif left != None and right == None: return False
|
||||||
|
elif left == None and right == None: return True
|
||||||
|
#排除了空节点,再排除数值不相同的情况
|
||||||
|
elif left.val != right.val: return False
|
||||||
|
|
||||||
|
#此时就是:左右节点都不为空,且数值相同的情况
|
||||||
|
#此时才做递归,做下一层的判断
|
||||||
|
outside = self.compare(left.left, right.right) #左子树:左、 右子树:右
|
||||||
|
inside = self.compare(left.right, right.left) #左子树:右、 右子树:左
|
||||||
|
isSame = outside and inside #左子树:中、 右子树:中 (逻辑处理)
|
||||||
|
return isSame
|
||||||
|
```
|
||||||
|
|
||||||
|
> 迭代法: 使用队列
|
||||||
|
```python
|
||||||
|
import collections
|
||||||
|
class Solution:
|
||||||
|
def isSymmetric(self, root: TreeNode) -> bool:
|
||||||
|
if not root:
|
||||||
|
return True
|
||||||
|
queue = collections.deque()
|
||||||
|
queue.append(root.left) #将左子树头结点加入队列
|
||||||
|
queue.append(root.right) #将右子树头结点加入队列
|
||||||
|
while queue: #接下来就要判断这这两个树是否相互翻转
|
||||||
|
leftNode = queue.popleft()
|
||||||
|
rightNode = queue.popleft()
|
||||||
|
if not leftNode and not rightNode: #左节点为空、右节点为空,此时说明是对称的
|
||||||
|
continue
|
||||||
|
|
||||||
|
#左右一个节点不为空,或者都不为空但数值不相同,返回false
|
||||||
|
if not leftNode or not rightNode or leftNode.val != rightNode.val:
|
||||||
|
return False
|
||||||
|
queue.append(leftNode.left) #加入左节点左孩子
|
||||||
|
queue.append(rightNode.right) #加入右节点右孩子
|
||||||
|
queue.append(leftNode.right) #加入左节点右孩子
|
||||||
|
queue.append(rightNode.left) #加入右节点左孩子
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
|
> 迭代法:使用栈
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def isSymmetric(self, root: TreeNode) -> bool:
|
||||||
|
if not root:
|
||||||
|
return True
|
||||||
|
st = [] #这里改成了栈
|
||||||
|
st.append(root.left)
|
||||||
|
st.append(root.right)
|
||||||
|
while st:
|
||||||
|
leftNode = st.pop()
|
||||||
|
rightNode = st.pop()
|
||||||
|
if not leftNode and not rightNode:
|
||||||
|
continue
|
||||||
|
if not leftNode or not rightNode or leftNode.val != rightNode.val:
|
||||||
|
return False
|
||||||
|
st.append(leftNode.left)
|
||||||
|
st.append(rightNode.right)
|
||||||
|
st.append(leftNode.right)
|
||||||
|
st.append(rightNode.left)
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
@ -281,6 +281,111 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
104.二叉树的最大深度
|
||||||
|
> 递归法:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def maxDepth(self, root: TreeNode) -> int:
|
||||||
|
return self.getDepth(root)
|
||||||
|
|
||||||
|
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
|
||||||
|
```
|
||||||
|
> 递归法;精简代码
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def maxDepth(self, root: TreeNode) -> int:
|
||||||
|
if not root:
|
||||||
|
return 0
|
||||||
|
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
|
||||||
|
```
|
||||||
|
|
||||||
|
> 迭代法:
|
||||||
|
```python
|
||||||
|
import collections
|
||||||
|
class Solution:
|
||||||
|
def maxDepth(self, root: TreeNode) -> int:
|
||||||
|
if not root:
|
||||||
|
return 0
|
||||||
|
depth = 0 #记录深度
|
||||||
|
queue = collections.deque()
|
||||||
|
queue.append(root)
|
||||||
|
while queue:
|
||||||
|
size = len(queue)
|
||||||
|
depth += 1
|
||||||
|
for i in range(size):
|
||||||
|
node = queue.popleft()
|
||||||
|
if node.left:
|
||||||
|
queue.append(node.left)
|
||||||
|
if node.right:
|
||||||
|
queue.append(node.right)
|
||||||
|
return depth
|
||||||
|
```
|
||||||
|
|
||||||
|
559.N叉树的最大深度
|
||||||
|
> 递归法:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def maxDepth(self, root: 'Node') -> int:
|
||||||
|
if not root:
|
||||||
|
return 0
|
||||||
|
depth = 0
|
||||||
|
for i in range(len(root.children)):
|
||||||
|
depth = max(depth, self.maxDepth(root.children[i]))
|
||||||
|
return depth + 1
|
||||||
|
```
|
||||||
|
|
||||||
|
> 迭代法:
|
||||||
|
```python
|
||||||
|
import collections
|
||||||
|
class Solution:
|
||||||
|
def maxDepth(self, root: 'Node') -> int:
|
||||||
|
queue = collections.deque()
|
||||||
|
if root:
|
||||||
|
queue.append(root)
|
||||||
|
depth = 0 #记录深度
|
||||||
|
while queue:
|
||||||
|
size = len(queue)
|
||||||
|
depth += 1
|
||||||
|
for i in range(size):
|
||||||
|
node = queue.popleft()
|
||||||
|
for j in range(len(node.children)):
|
||||||
|
if node.children[j]:
|
||||||
|
queue.append(node.children[j])
|
||||||
|
return depth
|
||||||
|
```
|
||||||
|
|
||||||
|
> 使用栈来模拟后序遍历依然可以
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def maxDepth(self, root: 'Node') -> int:
|
||||||
|
st = []
|
||||||
|
if root:
|
||||||
|
st.append(root)
|
||||||
|
depth = 0
|
||||||
|
result = 0
|
||||||
|
while st:
|
||||||
|
node = st.pop()
|
||||||
|
if node != None:
|
||||||
|
st.append(node) #中
|
||||||
|
st.append(None)
|
||||||
|
depth += 1
|
||||||
|
for i in range(len(node.children)): #处理孩子
|
||||||
|
if node.children[i]:
|
||||||
|
st.append(node.children[i])
|
||||||
|
|
||||||
|
else:
|
||||||
|
node = st.pop()
|
||||||
|
depth -= 1
|
||||||
|
result = max(result, depth)
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
@ -230,6 +230,56 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
> 递归法:前序遍历
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def invertTree(self, root: TreeNode) -> TreeNode:
|
||||||
|
if not root:
|
||||||
|
return None
|
||||||
|
root.left, root.right = root.right, root.left #中
|
||||||
|
self.invertTree(root.left) #左
|
||||||
|
self.invertTree(root.right) #右
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
|
> 迭代法:深度优先遍历(前序遍历)
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def invertTree(self, root: TreeNode) -> TreeNode:
|
||||||
|
if not root:
|
||||||
|
return root
|
||||||
|
st = []
|
||||||
|
st.append(root)
|
||||||
|
while st:
|
||||||
|
node = st.pop()
|
||||||
|
node.left, node.right = node.right, node.left #中
|
||||||
|
if node.right:
|
||||||
|
st.append(node.right) #右
|
||||||
|
if node.left:
|
||||||
|
st.append(node.left) #左
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
|
> 迭代法:广度优先遍历(层序遍历)
|
||||||
|
```python
|
||||||
|
import collections
|
||||||
|
class Solution:
|
||||||
|
def invertTree(self, root: TreeNode) -> TreeNode:
|
||||||
|
queue = collections.deque() #使用deque()
|
||||||
|
if root:
|
||||||
|
queue.append(root)
|
||||||
|
while queue:
|
||||||
|
size = len(queue)
|
||||||
|
for i in range(size):
|
||||||
|
node = queue.popleft()
|
||||||
|
node.left, node.right = node.right, node.left #节点处理
|
||||||
|
if node.left:
|
||||||
|
queue.append(node.left)
|
||||||
|
if node.right:
|
||||||
|
queue.append(node.right)
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
func invertTree(root *TreeNode) *TreeNode {
|
func invertTree(root *TreeNode) *TreeNode {
|
||||||
|
Reference in New Issue
Block a user