12
返回列表 发新帖
楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

狗哥店面,汗

🔗
旧未来 2020-8-22 05:02:12 | 只看该作者
全局:
匿名者 发表于 2020-8-22 04:02
对啊。,要保证每个node要么没有child 要么有两个child

lz你是怎么做的呀?
回复

使用道具 举报

🔗
titidada 2020-8-22 05:13:07 | 只看该作者
本楼:
全局:
祝福你過!
回复

使用道具 举报

🔗
onion2018 2020-8-22 14:59:39 | 只看该作者
全局:
zzgzzm 发表于 2020-8-21 23:36
第一问直接用top down 递归应该很容易解。

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

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

使用道具 举报

🔗
abcd1997 2020-8-24 03:23:18 | 只看该作者
全局:
请问是2021 SDE new grad么?网上找不到链接呀?
回复

使用道具 举报

🔗
坨坨 2020-9-8 03:28:23 | 只看该作者
全局:
这题第二问有啥好的办法吗
回复

使用道具 举报

🔗
BeTheBest 2020-9-8 15:25:46 | 只看该作者
全局:
楼主可以再讲下第2和3问的思路吗?多谢!
回复

使用道具 举报

🔗
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. }
复制代码
回复

使用道具 举报

🔗
copper1820 2021-3-10 07:52:29 | 只看该作者
全局:
oumizx 发表于 2020-9-23 19:10
[mw_shl_code=java,true]public class CalcTree {
    static class TreeNode {
        String val;

老哥不一定是完全二叉树吧
         +
      /    \
   -       5
/   \
3   2  
回复

使用道具 举报

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

本版积分规则

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