查看: 9928| 回复: 93
跳转到指定楼层
上一主题 下一主题
收起左侧

[高频题] 记录贴,互勉

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x

冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲

评分

参与人数 1大米 +1 收起 理由
小亩_296dcea + 1 一起加油

查看全部评分


上一篇:请教一下大家,递归该怎么写呀?
下一篇:弱问现在工业界python普遍用哪个版本
全局:
中序遍历只写出递归的方法,应该是不会被面试官轻易放过的。很可能让你写迭代的方法,两种都熟练面试官就很满意了。如果你能写出时间O(N) ,空间 O(1)的迭代方法就更好了
回复

使用道具 举报

推荐
 楼主| 微信用户_b99d1cc 2023-8-31 03:53:58 | 只看该作者
全局:
337. House Robber III
Medium
经典题目: take or not take
For the current root node, we can either take or not take, we use a recursive function to calculate the maximum value we can get, we start from the bottom to the top, if we take current node, the child couldn't be take, but the grandchild could be either take or not take.
In this question, we need to use memory to prevent multiple calculations.
As the bottom node already calculated once for its best result, we just need to check whether we take the root value or not take but the maximum value of its left and right child.

image.png (33.63 KB, 下载次数: 307)

image.png
回复

使用道具 举报

全局:
Uses a recursion helper function to call itself
94. Binary Tree Inorder Traversal
Left -> Root -> Right
144. Binary Tree Preorder Traversal
Root -> Left -> Right
145. Binary Tree Postorder Traversal
Left -> Right -> Root
429. N-ary Tree Level Order Traversal
Use Queue to  do level order, In order to get all the value from
the same level, we need to add all the children of the current root to the queue

补充内容 (2023-08-16 10:05 +08:00):

回复

使用道具 举报

🔗
 楼主| 微信用户_b99d1cc 2023-8-16 10:31:45 | 只看该作者
全局:
lishuzheng1012 发表于 2023-8-15 18:16
中序遍历只写出递归的方法,应该是不会被面试官轻易放过的。很可能让你写迭代的方法,两种都熟练面试官就很 ...

谢谢提醒
回复

使用道具 举报

🔗
 楼主| 微信用户_b99d1cc 2023-8-16 11:56:09 | 只看该作者
全局:
本帖最后由 微信用户_b99d1cc 于 2023-8-15 20:03 编辑
微信用户_b99d1cc 发表于 2023-8-15 18:00
Uses a recursion helper function to call itself
94. Binary Tree Inorder Traversal
Left -> Root ->  ...

94. Binary Tree Inorder Traversal    Space O(1)做法
Use stack to add all the current left node to the stack until hit the null, then add the current
value to the ans, then check the right subTree.

回复

使用道具 举报

🔗
 楼主| 微信用户_b99d1cc 2023-8-16 12:25:53 | 只看该作者
全局:
144. Binary Tree Preorder Traversal

image.png (20.43 KB, 下载次数: 356)

image.png
回复

使用道具 举报

全局:
这不是space O(1),给一颗一直往左走的树,这个代码第一件事就是把所有节点压栈里去,O(1)那个叫Morris Traversal
回复

使用道具 举报

🔗
 楼主| 微信用户_b99d1cc 2023-8-19 21:33:48 | 只看该作者
全局:
aodeyyoyo 发表于 2023-8-16 06:18
这不是space O(1),给一颗一直往左走的树,这个代码第一件事就是把所有节点压栈里去,O(1)那个叫Morris Tra ...

恩 后面发现问题了哈哈哈 太久没刷题了
回复

使用道具 举报

🔗
 楼主| 微信用户_b99d1cc 2023-8-19 22:27:49 | 只看该作者
全局:
429. N-ary Tree Level Order Traversal
Use Queue
589. N-ary Tree Preorder Traversal
use helper function (recursion)
回复

使用道具 举报

🔗
 楼主| 微信用户_b99d1cc 2023-8-19 22:51:21 | 只看该作者
全局:
微信用户_b99d1cc 发表于 2023-8-19 06:27
429. N-ary Tree Level Order Traversal
Use Queue
589. N-ary Tree Preorder Traversal

590. N-ary Tree Postorder Traversal
Recursive
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表