面试32题:

题目:从上到下打印二叉树

题:不分行从上到下打印二叉树

解题代码:

# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回从上到下每个节点值列表,例:[1,2,3]
def PrintFromTopToBottom(self, root):
# write code here
if not root:
return []
res=[]
res_val=[]
res.append(root)
while len(res)>0:
node=res.pop(0)
res_val.append(node.val)
if node.left:
res.append(node.left)
if node.right:
res.append(node.right)
return res_val

题目拓展一:分行从上到下打印二叉树。

题:从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

解题代码一:同剑指offer

# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回二维列表[[1,2],[4,5]]
def Print(self, pRoot):
# write code here
if not pRoot:
return []
res=[]
res_val=[]
res.append(pRoot) nextLevel=0 #表示下一层节点的数目
toBePrinted=1 #表示当前层还没有打印的节点数
temp=[]
while len(res)>0:
node=res[0]
temp.append(node.val)
if node.left:
res.append(node.left)
nextLevel+=1 if node.right:
res.append(node.right)
nextLevel+=1 del res[0]
toBePrinted-=1
if toBePrinted==0:
res_val.append(temp)
toBePrinted=nextLevel
nextLevel=0
temp=[]
return res_val

解题代码二:代码更简洁。

# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回二维列表[[1,2],[4,5]]
def Print(self, pRoot):
# write code here
if not pRoot:
return []
res,nodes=[],[pRoot]
while nodes:
curStack,nextStack=[],[]
for node in nodes:
curStack.append(node.val)
if node.left:
nextStack.append(node.left)
if node.right:
nextStack.append(node.right)
res.append(curStack)
nodes=nextStack
return res

解题代码三:cur、last记录,思路同二

# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回二维列表[[1,2],[4,5]]
def Print(self, pRoot):
# write code here
if not pRoot:
return []
res=[]
arr=[]
arr.append(pRoot)
cur=0
#last=1
while cur<len(arr):
last=len(arr)
temp=[]
while (cur<last):
temp.append(arr[cur].val)
if arr[cur].left:
arr.append(arr[cur].left)
if arr[cur].right:
arr.append(arr[cur].right)
cur+=1
res.append(temp)
return res

题目拓展二:之字形打印二叉树

题:请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

解题代码一:简洁。

# -*- 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=[]
nodes=[pRoot]
leftToRight=True while nodes:
curStack,nextStack=[],[]
for node in nodes:
curStack.append(node.val)
if node.left:
nextStack.append(node.left)
if node.right:
nextStack.append(node.right)
if not leftToRight:
curStack.reverse()
res.append(curStack)
leftToRight = not leftToRight
nodes=nextStack

解题代码二:同剑指offer。

# -*- 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=[]
nodes=[pRoot]
right=True while nodes:
curStack,nextStack=[],[]
if right:
for node in nodes:
curStack.append(node.val)
if node.left:
nextStack.append(node.left)
if node.right:
nextStack.append(node.right)
else:
for node in nodes:
curStack.append(node.val)
if node.right:
nextStack.append(node.right)
if node.left:
nextStack.append(node.left)
res.append(curStack)
nextStack.reverse()
right=not right
nodes=nextStack
return res
05-04 12:50