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

狗家电面新鲜面经

🔗
codemonk 2017-8-25 00:35:47 | 只看该作者
全局:
这次没贴错了
  1. struct TreeNode {
  2.     int val;
  3.     TreeNode *left, *right;
  4.     TreeNode(int val_) : val(val_), left(nullptr), right(nullptr) {};
  5. };

  6. int helper(TreeNode* root, int &add) {
  7.     if(!root) return 0;
  8.     if(!root->left && !root->right) return root->val;
  9.     int l = helper(root->left, add);
  10.     int r = helper(root->right, add);
  11.     if(!root->left) return r + root->val;
  12.     if(!root->right) return l + root->val;
  13.     add += abs(l - r);
  14.     return max(l, r) + root->val;
  15. }

  16. int minimumAdd(TreeNode* root) {
  17.     int add = 0;
  18.     helper(root, add);
  19.     return add;
  20. }

  21. void test1() {
  22.     TreeNode n1(1), n2(2), n3(3), n4(4);
  23.     n1.right = &n2;
  24.     n2.right = &n3;
  25.     n3.right = &n4;
  26.     cout << minimumAdd(&n1) << endl;
  27. }

  28. void test2() {
  29.     /*
  30.         1
  31.          \
  32.           2
  33.          / \
  34.         5   3
  35.              \
  36.               4
  37.     */
  38.     TreeNode n1(1), n2(2), n3(3), n4(4), n5(5);
  39.     n1.right = &n2;
  40.     n2.right = &n3;
  41.     n2.left = &n5;
  42.     n3.right = &n4;
  43.     cout << minimumAdd(&n1) << endl;
  44. }

  45. void test3() {
  46.     /*
  47.      1
  48.     / \
  49.    6   2
  50.       / \
  51.      5   3
  52.           \
  53.            4
  54.      */
  55.     TreeNode n1(1), n2(2), n3(3), n4(4), n5(5), n6(6);
  56.     n1.right = &n2;
  57.     n1.left = &n6;
  58.     n2.right = &n3;
  59.     n2.left = &n5;
  60.     n3.right = &n4;
  61.     cout << minimumAdd(&n1) << endl;
  62. }

  63. int main(int argc, const char * argv[]) {
  64.     // insert code here...
  65.     test1();
  66.     test2();
  67.     test3();
  68.     return 0;
  69. }
复制代码
回复

使用道具 举报

🔗
面试问题 2017-8-25 04:32:21 | 只看该作者
全局:
fenghao 发表于 2017-8-24 10:32
关键人家问你怎么调整,在哪个节点调整

调整的话就在那些不是max sum的path的leaf node上加不就行了?还是说有什么要求么
回复

使用道具 举报

🔗
yikehongxin 2017-8-25 05:37:56 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
面试问题 2017-8-25 06:25:53 | 只看该作者
全局:
daguanyuan 发表于 2017-8-25 05:37
那样就不是最优了。例如:
   1
  /  \

哦我也懂了 基本上是让每一层的和都一样
回复

使用道具 举报

🔗
baudelaire 2017-8-26 04:04:41 | 只看该作者
全局:
yugioh 发表于 2017-8-24 01:07
不太对,只有一个child的时候是不需要求diff加到全局的

多谢提醒
回复

使用道具 举报

🔗
baudelaire 2017-8-26 04:05:52 | 只看该作者
全局:
fenghao 发表于 2017-8-24 09:06
你这个肯定挂了,如果只有一个leaf你返回0,结果上层把diff加上去了。实际上应该返回None,上层知道只有 ...

嗯是的 掉入了假leaf陷阱 边界条件得卡到leaf上
回复

使用道具 举报

🔗
baudelaire 2017-8-26 04:15:29 | 只看该作者
全局:
之前贴的代码是错的 改了以后是这样:
  1. class Solution {
  2.     private int ans;
  3.     public int minAddToTree(TreeNode root){
  4.         // supposing the tree is binary tree
  5.         ans = 0;
  6.         feedTree(root);
  7.         return ans;
  8.     }
  9.     private int feedTree(TreeNode root){
  10.         // this funciton 'feed' the tree to make all root->leaf path sums equal
  11.         // return: path sum of the tree
  12.         if(root == null) return 0;
  13.         int leftPath = feedTree(root.left);
  14.         int rightPath = feedTree(root.right);
  15.         if(leftPath!=0 && rightPath!=0) // check if one child is null, if so no need to feed
  16.             ans += Math.max(leftPath, rightPath) - Math.min(leftPath, rightPath);
  17.         return Math.max(leftPath, rightPath) + root.val;
  18.     }
  19. }
复制代码
回复

使用道具 举报

🔗
lcq123 2017-8-26 10:21:22 | 只看该作者
全局:
请问,楼主代码是在Google doc上面打吗?谢谢!
回复

使用道具 举报

🔗
 楼主| fenghao 2017-8-26 15:56:20 | 只看该作者
全局:
lcq123 发表于 2017-8-26 10:21
请问,楼主代码是在Google doc上面打吗?谢谢!

是的,白板太不方便
回复

使用道具 举报

🔗
Arien 2017-9-2 03:17:45 | 只看该作者
全局:
请问楼主内推多久之后收到了店面通知呢?
回复

使用道具 举报

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

本版积分规则

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