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

狗哥店面,汗

🔗
匿名用户-U0ULC  2020-8-21 14:52:25 |倒序浏览
提示: 作者被禁止或删除 内容自动屏蔽

上一篇:狗家店面,感觉超难
下一篇:FB 新鲜出炉的店面,一脸懵
推荐
onion2018 2020-8-22 14:59:39 | 只看该作者
全局:
zzgzzm 发表于 2020-8-21 23:36
第一问直接用top down 递归应该很容易解。

请问这个删节点的规划具体是怎么定义的?因为只有叶节点为数值 ...

应该只是删除某个subtree之外的所有节点吧,简而言之就是找最大的某个subtree which subtree.val = target
回复

使用道具 举报

推荐
oumizx 2020-9-23 19:10:31 | 只看该作者
全局:
  1. public class CalcTree {
  2.     static class TreeNode {
  3.         String val;
  4.         TreeNode left;
  5.         TreeNode right;
  6.         public TreeNode(String val) {
  7.             this.val = val;
  8.             this.left = null;
  9.             this.right = null;
  10.         }
  11.     }

  12.     TreeNode root;
  13.     int sum;
  14.     int deepestDepth;
  15.     int minNumOfNodesToRemove;

  16.     public CalcTree(TreeNode root) {
  17.         this.root = root;
  18.         this.minNumOfNodesToRemove = Integer.MAX_VALUE;
  19.     }

  20.     public int cal() {
  21.         int res = helper(root);
  22.         this.sum = res;
  23.         return res;
  24.     }

  25.     public int helper(TreeNode root) {
  26.         if (root.left == null && root.right == null) {
  27.             return Integer.parseInt(root.val);
  28.         }

  29.         int leftVal = helper(root.left);
  30.         int rightVal = helper(root.right);
  31.         if ("+".equals(root.val)) {
  32.             return leftVal + rightVal;
  33.         } else {
  34.             return leftVal - rightVal;
  35.         }
  36.     }
  37.    
  38.     public int minRemoveToGet(int target) {
  39.         minNumOfNodesToRemove = Integer.MAX_VALUE;
  40.         helper2(root, target, 0);

  41.         return minNumOfNodesToRemove == Integer.MAX_VALUE ? -1 : minNumOfNodesToRemove;
  42.     }

  43.     private int helper2(TreeNode node, int target, int depth) {
  44.         if (node.left == null && node.right == null) {
  45.             // Based on the description we know it is a full binary tree. So total node number is 2 ^ (deepestDepth + 1) - 1.
  46.             deepestDepth = depth;
  47.             return Integer.parseInt(node.val);
  48.         }
  49.         int leftVal = helper2(node.left, target, depth + 1);
  50.         int rightVal = helper2(node.right, target, depth + 1);

  51.         int res = "+".equals(node.val) ? leftVal + rightVal : leftVal - rightVal;

  52.         if (res == target) {
  53.             int height = deepestDepth - depth + 1;
  54.             int totalNumOfNodes = (int) (Math.pow(2, deepestDepth + 1) - 1);
  55.             int curNumOfNodes = (int) (Math.pow(2, height) - 1);
  56.             minNumOfNodesToRemove = Math.min(minNumOfNodesToRemove, totalNumOfNodes - curNumOfNodes);
  57.         }

  58.         return res;
  59.     }

  60.     public static void main(String[] args) {
  61.         /**
  62.          *        +
  63.          *      +   -
  64.          *    6  5 4  3
  65.          */
  66.         TreeNode node1 = new TreeNode("+");
  67.         TreeNode node2 = new TreeNode("+");
  68.         TreeNode node3 = new TreeNode("-");
  69.         node1.left = node2;
  70.         node1.right = node3;
  71.         TreeNode node4 = new TreeNode("6");
  72.         TreeNode node5 = new TreeNode("5");
  73.         TreeNode node6 = new TreeNode("4");
  74.         TreeNode node7 = new TreeNode("3");
  75.         node2.left = node4;
  76.         node2.right = node5;
  77.         node3.left = node6;
  78.         node3.right = node7;
  79.         CalcTree solution = new CalcTree(node1);
  80.         System.out.println(solution.cal());
  81.         
  82.         System.out.println(solution.minRemoveToGet(11));
  83.         System.out.println(solution.minRemoveToGet(1));
  84.         System.out.println(solution.minRemoveToGet(13));


  85.     }
  86. }
复制代码
回复

使用道具 举报

推荐
旧未来 2020-8-21 23:48:40 | 只看该作者
全局:
本帖最后由 旧未来 于 2020-8-22 00:00 编辑

第一问应该是个post-order traversal就可以了
第二问 没有想到什么特别好的方法。。。同问一下删除的规则是怎么样的如果给的树是
        -
   -         +
1    +   3  4
  -5    6

原来的表达式是1 - (-5 +6)- (3 + 4)
可以只把6删除了吗?还是必须要把1,-5,6都给删除了
回复

使用道具 举报

🔗
djw612 2020-8-21 22:50:35 | 只看该作者
全局:
请问"二叉树组成的表达式"指的是什么呢?
回复

使用道具 举报

🔗
zzgzzm 2020-8-21 23:29:13 来自APP | 只看该作者
全局:
djw612 发表于 2020-08-21 07:50:35
请问"二叉树组成的表达式"指的是什么呢?
这个题是以二叉树的形式来表达一个串连的binary operations.

叶节点为input values, 非叶节点为binary operators (+ or -).
回复

使用道具 举报

🔗
zzgzzm 2020-8-21 23:36:59 来自APP | 只看该作者
全局:
第一问直接用top down 递归应该很容易解。

请问这个删节点的规划具体是怎么定义的?因为只有叶节点为数值,那么究竟怎么删才能保持“剩余”的树仍然是个合法的表达式?
回复

使用道具 举报

🔗
xhjennyz 2020-8-22 00:53:53 | 只看该作者
全局:
请问楼主投的是什么岗位呀
回复

使用道具 举报

🔗
rayluck4 2020-8-22 03:35:35 | 只看该作者
全局:
旧未来 发表于 2020-8-21 23:48
第一问应该是个post-order traversal就可以了
第二问 没有想到什么特别好的方法。。。同问一下删除的规则 ...

按对于子节点数量的要求,删6肯定得删5,那剩一个加号算什么:(1 - (+)) - (3 + 4), 当0处理吗?题目没有表达清楚
回复

使用道具 举报

🔗
旧未来 2020-8-22 03:56:13 | 只看该作者
全局:
匿名者 发表于 2020-8-22 03:41
删5,删6,然后把加号替换成6.

还是有点confuse, 如果是删除1呢?是变成这个样子吗?
        -
   +         +
-5   6   3  4

回复

使用道具 举报

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

本版积分规则

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