[刷题]Max Tree

[LintCode]Max Tree

Version 1 暴力 空间复杂度太高

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param A: Given an integer array with no duplicates.
     * @return: The root of max tree.
     */
    public TreeNode maxTree(int[] A) {
        // 2015-09-05 暴力
        if (A == null || A.length == 0) {
            return null;
        }
        int index = findMax(A);
        TreeNode root = new TreeNode(A[index]);
        if (0 < index) {
            int[] left = Arrays.copyOfRange(A, 0, index);
            root.left = maxTree(left);
        } else {
            root.left = null;
        }
        if (index < A.length - 1) {
            int[] right = Arrays.copyOfRange(A, index + 1, A.length);
            root.right = maxTree(right);
        } else {
            root.right = null;
        }
        return root;
    }
    
    /**
     * 返回序号
     */
    private int findMax(int[] A) {
        if (A == null || A.length == 0) {
            return -1;
        }
        int index = -1;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < A.length; i++) {
            if (max < A[i]) {
                index = i;
                max = A[i];
            }
        }
        return index;
    }
}

Version 2 暴力 StackOverflowError 题目给的数组太大

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param A: Given an integer array with no duplicates.
     * @return: The root of max tree.
     */
    public TreeNode maxTree(int[] A) {
        // 2015-09-05 暴力 
        if (A == null || A.length == 0) {
            return null;
        }
        
        return helper(A, 0, A.length - 1);
    }
    
    /**
     * 改进,降低空间复杂度
     */
    private TreeNode helper(int[] A, int start, int end) {
        if (start > end) {
            return null;
        }
        int index = findMax(A, start, end);
        TreeNode root = new TreeNode(A[index]);
        root.left = helper(A, start, index - 1);
        root.right = helper(A, index + 1, end);
        return root;
    }
    
    /**
     * 返回序号
     */
    private int findMax(int[] A, int start, int end) {
        if (start > end) {
            return -1;
        }
        int index = -1;
        int max = Integer.MIN_VALUE;
        for (int i = start; i <= end; i++) {
            if (max < A[i]) {
                index = i;
                max = A[i];
            }
        }
        return index;
    }
}

Version 3

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param A: Given an integer array with no duplicates.
     * @return: The root of max tree.
     */
    public TreeNode maxTree(int[] A) {  
        // 2015-09-05
        if (A == null || A.length == 0) {
            return null;
        }
        // 栈中点的值递减
        ArrayDeque<TreeNode> stack = new ArrayDeque<>();  
  
        // 循环len + 1次  
        for (int i = 0; i <= A.length; i++) {  
            // 假设在数组的最后加上第n+1个数,大小为正无穷  
            TreeNode curNode = (i == A.length) ? new TreeNode(Integer.MAX_VALUE)  : new TreeNode(A[i]);  
            
            // 一次循环pop一次
            while (!stack.isEmpty() && curNode.val > stack.peek().val) {  
                TreeNode popNode = stack.pop(); 
                // 下面分析将popNode放在哪
                if (stack.isEmpty()) {  
                    curNode.left = popNode;  
                } else {  
                    TreeNode leftPop = stack.peek();  
                    if (leftPop.val > curNode.val) {  
                        curNode.left = popNode;  
                    } else {  
                        leftPop.right = popNode;  
                    }  
                }  
            } // while
            stack.push(curNode);  
        }  
        // 最后push进去的是第n + 1个点  
        return stack.peek().left;  
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为您介绍一下 SQL Server 刷题的相关内容。SQL Server 是一种关系型数据库管理系统,用于存储和管理大量结构化数据。在刷题过程中,您可以通过解决一系列与 SQL Server 相关的问题来提高对其的理解和应用能力。 以下是一些常见的 SQL Server 刷题方向和相关问题: 1. SQL 基础知识: - 什么是 SQL Server?它有哪些特点和优势? - SQL Server 中的数据类型有哪些?它们分别用于存储什么类型的数据? - 如何创建数据库和表格? - 如何插入、更新和删除数据? - 如何使用 SELECT 语句查询数据? 2. 数据查询与筛 - 如何使用 WHERE 子句进行条件选? - 如何使用 ORDER BY 子句对查询结果进行排序? - 如何使用 GROUP BY 子句进行分组聚合操作? - 如何使用 HAVING 子句进行条件筛选(针对分组后的结果)? . 数据操作与修改: - 如何使用 UPDATE 语句修改表格中的数据? - 如何使用 DELETE 语句删除表格中的数据? - 如何使用 INSERT INTO 语句插入新的数据? 4. 数据连接与联结: - 如何使用 JOIN 连接多个表格? - 什么是内连接、外连接和交叉连接? - 如何使用子查询进行复杂的数据查询? 5. 数据聚合与统计: - 如何使用聚合函数(如 SUM、AVG、COUNT、MAX、MIN)进行数据统计? - 如何使用 GROUP BY 子句进行分组统计? - 如何使用 HAVING 子句进行条件筛选(针对分组后的统计结果)? 这些问题只是 SQL Server 刷题中的一部分,您可以根据自己的需求和水平选择适合的题目进行练习。同时,还可以参考 SQL Server 官方文档、教程和在线资源来深入学习和提高技能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值