1、对称的二叉树
题目描述
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
笔记:刚看到题目,想的思路是求出此二叉树的镜像二叉树,再判断,现在发现思路都是错的,题目中的关键是 “如果一个二叉树同此二叉树的镜像是一样的”
看评论区的笔记:mark
/*思路:首先根节点以及其左右子树,左子树的左子树和右子树的右子树相同
- 左子树的右子树和右子树的左子树相同即可,采用递归
- 非递归也可,采用栈或队列存取各级子树根节点
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetrical(self, pRoot):
# write code here
def is_same(p1,p2):
if not p1 and not p2:
return True
if (p1 and p2) and p1.val == p2.val:
return is_same(p1.left,p2.right) and is_same(p1.right,p2.left)
return False
if not pRoot:
return True
if not pRoot.left and pRoot.right:
return False
if not pRoot.right and pRoot.left:
return False
return is_same(pRoot.left,pRoot.right)
2、按之字形顺序打印二叉树
题目描述
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
笔记:很经典的题目,一般方法算法时间和空间复杂度高,看了解析,和高票答案,一共看了三种解答,如下
(链接:https://www.nowcoder.com/questionTerminal/91b69814117f4e8097390d107d2efbe0
来源:牛客网
大家的实现很多都是将每层的数据存进ArrayList中,偶数层时进行reverse操作,
- 在海量数据时,这样效率太低了。
- (我有一次面试,算法考的就是之字形打印二叉树,用了reverse,
- 直接被鄙视了,面试官说海量数据时效率根本就不行。)
- 下面的实现:不必将每层的数据存进ArrayList中,偶数层时进行reverse操作,直接按打印顺序存入
- 思路:利用Java中的LinkedList的底层实现是双向链表的特点。
-
1)可用做队列,实现树的层次遍历
-
2)可双向遍历,奇数层时从前向后遍历,偶数层时从后向前遍历)
解法1:
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def Print(self, pRoot):
# write code here
if not pRoot:
return []
from collections import deque
res,tmp = [],[]
last = pRoot
q = deque([pRoot])
left_to_right = True
while q:
t = q.popleft()
tmp.append(t.val)
if t.left:
q.append(t.left)
if t.right:
q.append(t.right)
if t == last:
res.append(tmp if left_to_right else tmp[::-1])
tmp = []
left_to_right = not left_to_right
if q: last = q[-1]
return res
解法2:
很巧妙地方法,思路很清晰,感觉这个更适合新手理解掌握
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def Print(self, pRoot):
# write code here
if not pRoot:
return []
res = []
odd = []
even = []
odd.append(pRoot)
while odd or even:
row = []
while odd:
tmp = odd.pop()
row.append(tmp.val)
if tmp.left:
even.append(tmp.left)
if tmp.right:
even.append(tmp.right)
if row:
res.append(row)
row = []
while even:
tmp = even.pop()
row.append(tmp.val)
if tmp.right:
odd.append(tmp.right)
if tmp.left:
odd.append(tmp.left)
if row:
res.append(row)
return res
解法3:
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def Print(self, root):
# write code here
if not root:
return []
c=[root]
f=[]
count=1
while len(c)>0:
d=[]
e=[]
for i in c:
if i!=None:
d.append(i.val)
if i.left:
e.append(i.left)
if i.right:
e.append(i.right)
if count%2==0:
d.reverse()
f.append(d)
c=e
count=count+1
return f
3、把二叉树打印成多行
题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
笔记:把上个题目中解法简化即可,任意一种
自己改的
# self.left = None
# self.right = None
class Solution:
# 返回二维列表[[1,2],[4,5]]
def Print(self, pRoot):
# write code here
if not pRoot:
return []
res = []
p = []
p.append(pRoot)
while len(p)>0:
c = []
d = []
while p:
tmp = p.pop(0)
d.append(tmp.val)
if tmp.left:
c.append(tmp.left)
if tmp.right:
c.append(tmp.right)
res.append(d)
p = c
return res