Skip to content

Commit 349bab7

Browse files
Merge pull request #323 from z80160280/master
添加 0226 0101 0104 python版本
2 parents c586e00 + c44a316 commit 349bab7

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed

problems/0101.对称二叉树.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,78 @@ Java:
360360

361361
Python:
362362

363+
> 递归法
364+
```python
365+
class Solution:
366+
def isSymmetric(self, root: TreeNode) -> bool:
367+
if not root:
368+
return True
369+
return self.compare(root.left, root.right)
370+
371+
def compare(self, left, right):
372+
#首先排除空节点的情况
373+
if left == None and right != None: return False
374+
elif left != None and right == None: return False
375+
elif left == None and right == None: return True
376+
#排除了空节点,再排除数值不相同的情况
377+
elif left.val != right.val: return False
378+
379+
#此时就是:左右节点都不为空,且数值相同的情况
380+
#此时才做递归,做下一层的判断
381+
outside = self.compare(left.left, right.right) #左子树:左、 右子树:右
382+
inside = self.compare(left.right, right.left) #左子树:右、 右子树:左
383+
isSame = outside and inside #左子树:中、 右子树:中 (逻辑处理)
384+
return isSame
385+
```
386+
387+
> 迭代法: 使用队列
388+
```python
389+
import collections
390+
class Solution:
391+
def isSymmetric(self, root: TreeNode) -> bool:
392+
if not root:
393+
return True
394+
queue = collections.deque()
395+
queue.append(root.left) #将左子树头结点加入队列
396+
queue.append(root.right) #将右子树头结点加入队列
397+
while queue: #接下来就要判断这这两个树是否相互翻转
398+
leftNode = queue.popleft()
399+
rightNode = queue.popleft()
400+
if not leftNode and not rightNode: #左节点为空、右节点为空,此时说明是对称的
401+
continue
402+
403+
#左右一个节点不为空,或者都不为空但数值不相同,返回false
404+
if not leftNode or not rightNode or leftNode.val != rightNode.val:
405+
return False
406+
queue.append(leftNode.left) #加入左节点左孩子
407+
queue.append(rightNode.right) #加入右节点右孩子
408+
queue.append(leftNode.right) #加入左节点右孩子
409+
queue.append(rightNode.left) #加入右节点左孩子
410+
return True
411+
```
412+
413+
> 迭代法:使用栈
414+
```python
415+
class Solution:
416+
def isSymmetric(self, root: TreeNode) -> bool:
417+
if not root:
418+
return True
419+
st = [] #这里改成了栈
420+
st.append(root.left)
421+
st.append(root.right)
422+
while st:
423+
leftNode = st.pop()
424+
rightNode = st.pop()
425+
if not leftNode and not rightNode:
426+
continue
427+
if not leftNode or not rightNode or leftNode.val != rightNode.val:
428+
return False
429+
st.append(leftNode.left)
430+
st.append(rightNode.right)
431+
st.append(leftNode.right)
432+
st.append(rightNode.left)
433+
return True
434+
```
363435

364436
Go:
365437

problems/0104.二叉树的最大深度.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,111 @@ class Solution {
281281

282282
Python:
283283

284+
104.二叉树的最大深度
285+
> 递归法:
286+
```python
287+
class Solution:
288+
def maxDepth(self, root: TreeNode) -> int:
289+
return self.getDepth(root)
290+
291+
def getDepth(self, node):
292+
if not node:
293+
return 0
294+
leftDepth = self.getDepth(node.left) #左
295+
rightDepth = self.getDepth(node.right) #右
296+
depth = 1 + max(leftDepth, rightDepth) #中
297+
return depth
298+
```
299+
> 递归法;精简代码
300+
```python
301+
class Solution:
302+
def maxDepth(self, root: TreeNode) -> int:
303+
if not root:
304+
return 0
305+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
306+
```
307+
308+
> 迭代法:
309+
```python
310+
import collections
311+
class Solution:
312+
def maxDepth(self, root: TreeNode) -> int:
313+
if not root:
314+
return 0
315+
depth = 0 #记录深度
316+
queue = collections.deque()
317+
queue.append(root)
318+
while queue:
319+
size = len(queue)
320+
depth += 1
321+
for i in range(size):
322+
node = queue.popleft()
323+
if node.left:
324+
queue.append(node.left)
325+
if node.right:
326+
queue.append(node.right)
327+
return depth
328+
```
329+
330+
559.N叉树的最大深度
331+
> 递归法:
332+
```python
333+
class Solution:
334+
def maxDepth(self, root: 'Node') -> int:
335+
if not root:
336+
return 0
337+
depth = 0
338+
for i in range(len(root.children)):
339+
depth = max(depth, self.maxDepth(root.children[i]))
340+
return depth + 1
341+
```
342+
343+
> 迭代法:
344+
```python
345+
import collections
346+
class Solution:
347+
def maxDepth(self, root: 'Node') -> int:
348+
queue = collections.deque()
349+
if root:
350+
queue.append(root)
351+
depth = 0 #记录深度
352+
while queue:
353+
size = len(queue)
354+
depth += 1
355+
for i in range(size):
356+
node = queue.popleft()
357+
for j in range(len(node.children)):
358+
if node.children[j]:
359+
queue.append(node.children[j])
360+
return depth
361+
```
362+
363+
> 使用栈来模拟后序遍历依然可以
364+
```python
365+
class Solution:
366+
def maxDepth(self, root: 'Node') -> int:
367+
st = []
368+
if root:
369+
st.append(root)
370+
depth = 0
371+
result = 0
372+
while st:
373+
node = st.pop()
374+
if node != None:
375+
st.append(node) #中
376+
st.append(None)
377+
depth += 1
378+
for i in range(len(node.children)): #处理孩子
379+
if node.children[i]:
380+
st.append(node.children[i])
381+
382+
else:
383+
node = st.pop()
384+
depth -= 1
385+
result = max(result, depth)
386+
return result
387+
```
388+
284389

285390
Go:
286391

problems/0226.翻转二叉树.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,56 @@ class Solution {
230230

231231
Python:
232232

233+
> 递归法:前序遍历
234+
```python
235+
class Solution:
236+
def invertTree(self, root: TreeNode) -> TreeNode:
237+
if not root:
238+
return None
239+
root.left, root.right = root.right, root.left #中
240+
self.invertTree(root.left) #左
241+
self.invertTree(root.right) #右
242+
return root
243+
```
244+
245+
> 迭代法:深度优先遍历(前序遍历)
246+
```python
247+
class Solution:
248+
def invertTree(self, root: TreeNode) -> TreeNode:
249+
if not root:
250+
return root
251+
st = []
252+
st.append(root)
253+
while st:
254+
node = st.pop()
255+
node.left, node.right = node.right, node.left #中
256+
if node.right:
257+
st.append(node.right) #右
258+
if node.left:
259+
st.append(node.left) #左
260+
return root
261+
```
262+
263+
> 迭代法:广度优先遍历(层序遍历)
264+
```python
265+
import collections
266+
class Solution:
267+
def invertTree(self, root: TreeNode) -> TreeNode:
268+
queue = collections.deque() #使用deque()
269+
if root:
270+
queue.append(root)
271+
while queue:
272+
size = len(queue)
273+
for i in range(size):
274+
node = queue.popleft()
275+
node.left, node.right = node.right, node.left #节点处理
276+
if node.left:
277+
queue.append(node.left)
278+
if node.right:
279+
queue.append(node.right)
280+
return root
281+
```
282+
233283
Go:
234284
```Go
235285
func invertTree(root *TreeNode) *TreeNode {

0 commit comments

Comments
 (0)