
Leetcode 654.最大二叉树 Maximum Binary Tree(Java)
发布日期:2021-05-12 16:25:57
浏览次数:18
分类:精选文章
本文共 4330 字,大约阅读时间需要 14 分钟。
���������������������������������������������������������������������������������������������������������������������������������������������
������������
������������
- ������: ������������������������������������������������������������������������������������������������
- ������: ������������������������������
- ������: ��������������������������� O(n��)������������������������������������������������������������
ST���������
- ������: ���������������ST���������������O(1)������������������������������������������������������ST���������������������������������������
- ������: ������������������O(n log n)������������������������O(n log n)���
- ������: ���������������������������������ST��������� O(n log n)������������������������������������������������������������
���������
- ������: ���������������������������������������������������������������������������������������������������������������������������
- ������: ������������������ O(n)������������������������������������������
- ������: ���������������������������������������������������
������������
- ���������������: ������������������������������������������ O(n) ���������������������������������
- ���������������: ������������������������������������������������������
- ������������: ���������������������������������������������������������������������������������������
������������������
���������������������
class Solution { public TreeNode constructMaximumBinaryTree(int[] nums) { return build(nums, 0, nums.length - 1); } private TreeNode build(int[] nums, int l, int r) { if (l > r) return null; int maxVal = Integer.MIN_VALUE; int maxIndex = -1; for (int i = l; i <= r; i++) { if (nums[i] > maxVal) { maxVal = nums[i]; maxIndex = i; } } TreeNode root = new TreeNode(maxVal); root.left = build(nums, l, maxIndex - 1); root.right = build(nums, maxIndex + 1, r); return root; }}
ST������������������
class Solution { int[][] f; // ���������������ST��� int n, k; public TreeNode constructMaximumBinaryTree(int[] nums) { n = nums.length; k = (int) (Math.log(n) / Math.log(2)); f = new int[n][k + 1]; for (int j = 0; j <= k; j++) { for (int i = 0; i + (1 << j) - 1 < n; i++) { if (j == 0) { f[i][j] = i; } else { int left = f[i][j - 1], right = f[i + (1 << (j - 1))][j - 1]; f[i][j] = nums[left] > nums[right] ? left : right; } } } return build(nums, 0, n - 1); } private int query(int[] nums, int l, int r) { int len = r - l + 1; int k = (int) (Math.log(len) / Math.log(2)); if (l == r) return l; int a = f[l][k], b = f[r - (1 << k) + 1][k]; return nums[a] > nums[b] ? a : b; } private TreeNode build(int[] nums, int l, int r) { if (l > r) return null; int temp = query(nums, l, r); TreeNode root = new TreeNode(nums[temp]); root.left = build(nums, l, temp - 1); root.right = build(nums, temp + 1, r); return root; }}
������������������
import java.util.Deque;import java.util.LinkedList;class Solution { Dequestack; public TreeNode constructMaximumBinaryTree(int[] nums) { TreeNode curr = null; for (int i = 0; i < nums.length; i++) { curr = new TreeNode(nums[i]); while (!stack.isEmpty() && stack.peek().val < nums[i]) { curr.left = stack.pop(); if (stack.isEmpty() || stack.peek().val < curr.val) { stack.peekLast().right = curr; } } if (!stack.isEmpty()) { stack.peek().right = curr; } stack.push(curr); } return stack.isEmpty() ? null : stack.removeLast(); }}
������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年04月08日 09时50分17秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
CMCC登录参数分析
2021-05-14
win7一激活就蓝屏
2021-05-14
开源镜像站最新动态20150626
2021-05-14
开源镜像站最新动态20150723
2021-05-14
编译Windows32位的redis
2021-05-14
GridView的另外一种分页方式,可提高加载速度
2021-05-14
好好学习设计模式之一:外观模式
2021-05-14
基于异步方式的语法着色器
2021-05-14
委托-利用GetInvocationList处理链式委托
2021-05-14
var that = this 小坑记
2021-05-14
一些错误记录
2021-05-14
从客户端中检测到有潜在危险的request.form值
2021-05-14
GridView自定义删除操作
2021-05-14
http常见响应状态码
2021-05-14
Nginx Location
2021-05-14
计算机操作系统之第二章 进程的描述与控制(1)
2021-05-14
教资之教育知识与能力 第二章第三节 基础教育课程改革
2021-05-14
androird 优秀新闻github
2021-05-14
java 正则 持续更新中
2021-05-14