📣 独立日限时特惠: VIP通行证立减$68
查看: 1016| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[高频题] 105 求debug - 詭異的bug!

全局:

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

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

x
  1. class Solution {
  2.    
  3.     public TreeNode buildTree(int[] preorder, int[] inorder) {
  4.       
  5.         Map<Integer, Integer> inMap = new HashMap<>();
  6.         
  7.         int len = inorder.length;
  8.          
  9.         for(int i = 0; i < inorder.length; i++){
  10.             
  11.             inMap.put(inorder[i], i);
  12.         }
  13.         
  14.         TreeNode root = helper(preorder, 0, len - 1, inorder, 0, len - 1, inMap);
  15.          
  16.         return root;
  17.     }
  18.    
  19.     public TreeNode helper(int[] pre, int preStart, int preEnd,
  20.                            int[] in, int inStart, int inEnd,
  21.                            Map<Integer, Integer> inMap)
  22.     {
  23.         
  24.         System.out.println(preStart + "***");
  25.         
  26.         if(preStart > preEnd || inStart > inEnd) return null;  
  27.         
  28.         int val = pre[preStart];
  29.          
  30.         System.out.println(val);
  31.          
  32.         TreeNode root = new TreeNode( pre[preStart] );
  33.         
  34.         int inPosition = inMap.get( pre[preStart] );
  35.         
  36.         int leftRange = inPosition - preStart;
  37.         
  38.         root.left =  helper( pre, preStart + 1,              preStart + leftRange,
  39.                              in,  inStart,                   inPosition - 1,
  40.                              inMap);
  41.          
  42.         root.right = helper( pre, preStart + leftRange + 1,   preEnd,
  43.                              in,  inPosition + 1,             inEnd,
  44.                              inMap);         
  45.         
  46.         return root;
  47.     }
  48. }
复制代码



感謝各位!!!

上一篇:Google Mock Interview
下一篇:LeetCode不刷SQL可以吗
全局:
class Solution {

    public TreeNode buildTree(int[] preorder, int[] inorder) {

        Map<Integer, Integer> inMap = new HashMap<>();

        int len = inorder.length;

        for(int i = 0; i < inorder.length; i++){

            inMap.put(inorder, i);
        }

        TreeNode root = helper(preorder, 0, len - 1, inorder, 0, len - 1, inMap);

        return root;
    }

    public TreeNode helper(int[] pre, int preStart, int preEnd,
                           int[] in, int inStart, int inEnd,
                           Map<Integer, Integer> inMap)
    {

        System.out.println(preStart + "***");

        if(preStart > preEnd || inStart > inEnd) return null;  

        int val = pre[preStart];

        System.out.println(val);

        TreeNode root = new TreeNode( pre[preStart] );

        int inPosition = inMap.get( pre[preStart] );

        int leftRange = inPosition - inStart;

        root.left =  helper( pre, preStart + 1,              preStart + leftRange,
                             in,  inStart,                   inPosition - 1,
                             inMap);

        root.right = helper( pre, preStart + leftRange + 1,   preEnd,
                             in,  inPosition + 1,             inEnd,
                             inMap);         

        return root;
    }
}

第36行leftRange写错了

评分

参与人数 1大米 +3 收起 理由
不知道小帅 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| techDiscussion 2021-1-3 15:48:24 | 只看该作者
全局:
格林匹施ZELQ 发表于 2021-1-3 15:18
class Solution {

    public TreeNode buildTree(int[] preorder, int[] inorder) {

感謝同學,你救了我一命。。。。。
回复

使用道具 举报

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

本版积分规则

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