📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 973| 回复: 10
跳转到指定楼层
上一主题 下一主题
收起左侧

刷题记录贴

全局:

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

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

x
11/21/2023
Binary Tree Preorder Traversal
1. stack, end condition moved to prune condition
2. stack, first in last out, so put in left last instead of first

补充内容 (2023-11-22 12:07 +08:00):

589. N-ary Tree Preorder Traversal
python reverse a list
a[::-1] or a.reverse()
stack: check node as return is also fine





补充内容 (2023-11-22 12:09 +08:00):
reversed(cur.children) rather than cur.children.reverse()
because reversed() can return reversed list but .reverse() will return None

补充内容 (2023-11-22 12:52 +08:00):

94. Binary Tree Inorder Traversal
stack:
  • while stack or pointer
  • move pointer, do not forget to turn right at last


补充内容 (2023-11-22 13:07 +08:00):

post order traversal:
stack
reverse pre order traversal in two ways:
how to put in stack
how to return res by res[::-1]

补充内容 (2023-11-22 13:46 +08:00):

level traversal:
extra param level
dfs: add new [] depending on level and len(res)
bfs: need to check len(q)

上一篇:已工作一年 跳槽打卡刷题
下一篇:pramp interview体验
🔗
 楼主| littleprayer 2023-11-24 03:57:55 | 只看该作者
全局:
11/22/2023 (Day2)

Today's goal is to finish the preorder section
100. Same Tree
DFS: 1. no need to check single node case, 2. no need helper
BFS: 1. use tuple for queue to avoid two queues, 2. prunning at the child case for tree might be more verbose, 3. return condition is the same as DFS

101. Symmetric Tree
helper is also isMirror(q, p), helper can change the input but not change meaning, isSymmetric(root)

572. Subtree of Another Tree
子树必须保证所有节点包括null节点也相同,所以需要理解清楚题意

226. Invert Binary Tree
do not forget to use temp for swap

111. Minimum Depth of Binary Tree
DB idea: two conditions, one case is one of the subtree is none, another case is both of the subtrees are not none
Regular idea: python syntax if any(node.children),  

Populating Next Right Pointers in Each Node
BFS: level traversal, cur pop within for loop rather than while
DFS: because of full tree, pre order makes sure that node.next must have been checked

Balanced Binary Tree
DFS max height for height
回复

使用道具 举报

🔗
 楼主| littleprayer 2023-11-24 13:35:34 | 只看该作者
全局:
11/23/2024
path sum
stack need to put three element tuple
path sum III (convert complext to simple case) has to include the current node
回复

使用道具 举报

🔗
 楼主| littleprayer 2023-11-25 12:58:36 | 只看该作者
全局:
11/24/2023 post order traversal
计算的前提是必须要有子叶子的结果,所以用post order
learned some python
1) self para do not put outside of the function
2) iterate dict as dict.items()
回复

使用道具 举报

🔗
 楼主| littleprayer 2023-11-27 05:11:54 来自APP | 只看该作者
全局:
11/25/2023
python insert the first element to arr is arr.insert(0, element)
level order traversal does not always requires bfs, dfs with level also works, Also, note the essense is to return index, pay attention to post order traverse or pre order traverse, also level order, left or right first also matters

补充内容 (2023-11-27 12:20 +08:00):

max width of tree: pos * 2, pos * 2 + 1 is how we get the position index, also max is initialized as 1 rather than 0, also need to set start for each level
回复

使用道具 举报

🔗
 楼主| littleprayer 2023-11-28 06:33:27 | 只看该作者
全局:
本帖最后由 littleprayer 于 2023-11-27 17:38 编辑

11/27/2023
python sort
arr.sort(key = lambda x:x[0])

for left boundry, right boudnary:
1. enter the helper function by checking node because we do not want to check the condition in prunning step
2. left boundary pre order, right boundary post order (because reverse order)
3. not enter sub functions in all conditions, depending whether or not that node is null
回复

使用道具 举报

🔗
 楼主| littleprayer 2023-11-30 12:54:59 | 只看该作者
全局:
11/28 finished tree
1. define sub function is key
2. arr.pop(0)
回复

使用道具 举报

🔗
 楼主| littleprayer 2023-12-1 15:36:09 来自APP | 只看该作者
全局:
11/29 看来一个系统设计的视频 看完了 dfs grid. 明天coding, 明天争取coding完成,再看一个系统设计视频
回复

使用道具 举报

🔗
 楼主| littleprayer 2023-12-3 13:45:00 | 只看该作者
全局:
finished rate limiter 12/02/2023
回复

使用道具 举报

🔗
 楼主| littleprayer 2023-12-4 12:48:45 | 只看该作者
全局:
12/2/2023
set operation: -, |, union, intersection
BFS out of memory issue: do not build directly, instead build secondary, also 'seen' store secondary
回复

使用道具 举报

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

本版积分规则

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