码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 力扣:654. 最大二叉树


    其实只需要每一次遍历的时候,找到最大的数组下标就行了。然后遍历既可以得到结果

    1. package com.算法专练.力扣.最大二叉树;
    2. import java.util.Arrays;
    3. /**
    4. * @author xnl
    5. * @Description:
    6. * @date: 2022/8/20 22:05
    7. */
    8. public class Solution {
    9. public static void main(String[] args) {
    10. Solution solution = new Solution();
    11. int[] arr = {3,2,1,6,0,5};
    12. System.out.println(solution.constructMaximumBinaryTree(arr));
    13. }
    14. public TreeNode constructMaximumBinaryTree(int[] nums) {
    15. if (nums == null || nums.length ==0){
    16. return null;
    17. }
    18. int maxIndex = 0;
    19. int maxValue = nums[0];
    20. for (int i = 1; i < nums.length; i++){
    21. if (nums[i] > maxValue){
    22. maxValue = nums[i];
    23. maxIndex = i;
    24. }
    25. }
    26. TreeNode node = new TreeNode(maxValue);
    27. node.left = constructMaximumBinaryTree(Arrays.copyOfRange(nums, 0, maxIndex));
    28. node.right = constructMaximumBinaryTree(Arrays.copyOfRange(nums, maxIndex + 1, nums.length));
    29. return node;
    30. }
    31. }
    32. class TreeNode {
    33. int val;
    34. TreeNode left;
    35. TreeNode right;
    36. TreeNode() {}
    37. TreeNode(int val) { this.val = val; }
    38. TreeNode(int val, TreeNode left, TreeNode right) {
    39. this.val = val;
    40. this.left = left;
    41. this.right = right;
    42. }
    43. }

    单调栈解法

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public TreeNode constructMaximumBinaryTree(int[] nums) {
    18. Deque deque = new ArrayDeque<>();
    19. for (int i = 0; i < nums.length; i++){
    20. TreeNode node = new TreeNode(nums[i]);
    21. while (!deque.isEmpty()){
    22. TreeNode top = deque.peek();
    23. if (top.val > node.val){
    24. deque.push(node);
    25. top.right = node;
    26. break;
    27. }
    28. node.left = deque.poll();
    29. }
    30. if (deque.isEmpty()) deque.push(node);
    31. }
    32. return deque.peekLast();
    33. }
    34. }

  • 相关阅读:
    【Verilog基础】8.加法器
    WiFi cfg80211
    Python学习记录 面向对象编程
    热烈祝贺|天栩(广东)有限公司受邀参加2022世界滋补产业生态大会
    【数据分享】2006-2021年我国城市级别的道路、桥梁、管线建设相关指标(10多项指标)
    GBase 8c在结果集中定位方法
    C语言-数据类型
    python爬虫入门(六)BeautifulSoup使用
    vue组件的重新渲染的问题
    单目标应用:麻雀搜索算法(SSA)优化RBF神经网络实现数据预测(RBF隐藏层神经元个数可以自行设定)
  • 原文地址:https://blog.csdn.net/newOneObject/article/details/126445259
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号