回复: 8
跳转到指定楼层
上一主题 下一主题
收起左侧

Doordash MLE 跪经

全局:

2021(10-12月) MachineLearningEng 博士 全职@doordash - 网上海投 - 技术电面  | 😐 Neutral 😣 Hard | Other | 在职跳槽

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

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

x
树的题目:叶子结点有asterisk的也有非asterisk的,找到两个asterisk 结点之间最大的path sum。例子
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
刷题,有好的solution建议嘛?希望下次还能碰到~


评分

参与人数 2大米 +5 收起 理由
sccnju + 1 赞一个
匿名用户-1KTYU + 4

查看全部评分


上一篇:fb ng pho screen
下一篇:购物网站一杯VO面经
推荐
niubuzhi 2021-10-26 09:24:40 | 只看该作者
全局:
本帖最后由 niubuzhi 于 2021-10-25 21:25 编辑

Super easy solution using Python (please add rice if you like it !!!)
  1. class TreeNode(object):
  2.     def __init__(self, val=0, left=None, right=None, isAsterisk=True):
  3.         self.val = val
  4.         self.left = left
  5.         self.right = right
  6.         self.is_asterisk = isAsterisk
  7.         
  8. class Solution(object):
  9.    
  10.     def _maxPathSum(self, root):
  11.         # this function only returns path sum using the root, and not using both childs at the same time
  12.         if not root:
  13.             return -sys.maxint
  14.         l = max(0, self._maxPathSum(root.left))
  15.         r = max(0, self._maxPathSum(root.right))
  16.         
  17.         current = root.val
  18.         if root.left is None and root.right is None and not root.is_asterisk:
  19.             # convert current node value from int to float, which will be used as an indicator of non-asterisk leaves in path
  20.             current += 0.0
  21.         if isinstance(l, int) and isinstance(r, int):
  22.             self.ans = max(self.ans, l + r + current)
  23.         return current + max(l, r)
  24.    
  25.     def maxPathSum(self, root):
  26.         import sys
  27.         self.ans = -sys.maxint
  28.         self._maxPathSum(root)
  29.         return self.ans
复制代码
回复

使用道具 举报

推荐
xiaoshai12 2021-9-29 05:04:48 | 只看该作者
全局:
Tree DFS 可以做这个
回复

使用道具 举报

推荐
cxq920423 2021-9-28 07:24:37 | 只看该作者
全局:
感谢楼主分享,可以refer这套题。 https://leetcode.com/problems/binary-tree-maximum-path-sum/
回复

使用道具 举报

🔗
Yarakk 2021-9-28 07:45:08 | 只看该作者
全局:
力扣八路无应该能做
回复

使用道具 举报

🔗
goodluckall 2021-9-30 06:37:22 | 只看该作者
全局:
Yarakk 发表于 2021-9-27 16:45
力扣八路无应该能做

请问是865吗?
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-UKOOV  2021-10-14 02:12:35
先遍历树找到带星号的结点,再找出两两的LCA,然后再算LCA到这两个点的sum,取所有可能的最大值?
回复

使用道具 举报

🔗
superIdiot 2021-10-20 12:32:49 | 只看该作者
全局:
这个是不是比124要简单些?只求叶子结点有星号的LCA最大就好了,不需要考虑树中间的结点
回复

使用道具 举报

🔗
Jacky208 2021-10-20 13:56:36 | 只看该作者
全局:
Java
  1.    class Solution {
  2.         int max;

  3.         public int maxPathSum(TreeNode root) {
  4.             max = Integer.MIN_VALUE;
  5.             dfs(root);
  6.             return max;
  7.         }

  8.         Integer dfs(TreeNode node) {
  9.             if (node == null) {
  10.                 return null;
  11.             }

  12.             Integer left = dfs(node.left);
  13.             Integer right = dfs(node.right);

  14.             if (left == null && right == null) {
  15.                 if (node.asterisk) {
  16.                     max = Math.max(max, node.val);
  17.                     return node.val;
  18.                 } else {
  19.                     return null;
  20.                 }
  21.             } else if (left == null) {

  22.                 int tmp = Math.max(node.val, node.val + right.intValue());
  23.                 max = Math.max(max, tmp);
  24.                 return tmp;
  25.             } else if (right == null) {
  26.                 int tmp = Math.max(node.val, node.val + left.intValue());
  27.                 max = Math.max(max, tmp);
  28.                 return tmp;
  29.             }
  30.             int leftValue = left.intValue();
  31.             int rightValue = right.intValue();
  32.             if (leftValue > 0 && right > 0) {
  33.                 max = Math.max(max, node.val + leftValue + rightValue);
  34.             }

  35.             if (leftValue > 0 || rightValue > 0) {
  36.                 int tmp = Math.max(node.val + leftValue, node.val + rightValue);
  37.                 max = Math.max(max, tmp);
  38.                 return tmp;
  39.             } else {
  40.                 max = Math.max(max, node.val);
  41.                 return node.val;
  42.             }
  43.         }
  44.     }
复制代码
回复

使用道具 举报

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

本版积分规则

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