2026年软件工程师面试题集及参考答案.docxVIP

  • 1
  • 0
  • 约5.26千字
  • 约 16页
  • 2026-03-11 发布于福建
  • 举报

2026年软件工程师面试题集及参考答案.docx

第PAGE页共NUMPAGES页

2026年软件工程师面试题集及参考答案

一、编程基础(共5题,每题10分,总分50分)

1.题目:

编写一个函数,实现二叉树的前序遍历(根-左-右)。要求使用递归和非递归两种方式分别实现,并说明时间复杂度和空间复杂度。

参考答案:

递归方式:

python

defpreorder_recursive(root):

ifnotroot:

return[]

result=[]

result.append(root.val)

result.extend(preorder_recursive(root.left))

result.extend(preorder_recursive(root.right))

returnresult

时间复杂度:O(n),空间复杂度:O(h),其中h为二叉树的高度。

非递归方式:

python

defpreorder_non_recursive(root):

ifnotroot:

return[]

stack,result=[root],[]

whilestack:

node=stack.pop()

result.append(node.val)

ifnode.right:

stack.append(node.right)

ifnode.left:

stack.appe

文档评论(0)

1亿VIP精品文档

相关文档