【lc刷题 day4】栈的压入、弹出序列 从上到下打印二叉树 二叉搜索树的后序遍历数列

news/2024/10/21 10:09:39/

剑指offer 31.栈的压入、弹出序列 medium

class Solution {public boolean validateStackSequences(int[] pushed, int[] popped) {Stack<Integer> s=new Stack<>();int i=0;for(int j=0;j<pushed.length;j++){s.push(pushed[j]);while(!s.isEmpty()&&popped[i]==s.peek()){s.pop();i++;}}return s.isEmpty();}
}

一开始没有思路,看题解才写出来的
建一个栈,把pushed中的元素都压进去,如果与poped中对应就弹出
时间复杂度O(n),空间复杂度O(n)

剑指Offer 32-I.从上到下打印为二叉树 medium

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {List<List<Integer>> list=new ArrayList<>();public int[] levelOrder(TreeNode root) {level(0,root);int count=0;for(int i=0;i<list.size();i++){count+=list.get(i).size();}int[] res=new int[count];int k=0;for(int i=0;i<list.size();i++){for(int j=0;j<list.get(i).size();j++){res[k]=list.get(i).get(j);k++;}}return res;}void level(int depth,TreeNode node){if(node==null) return;if(list.size()<=depth){List<Integer> l=new ArrayList<>();list.add(l);}list.get(depth).add(node.val);level(depth+1,node.left);level(depth+1,node.right);}
}

重点是建立一个双层链表List<List<Integer>> list
每一层的元素存在一个链表中
时间复杂度O(n),空间复杂度O(n)

剑指Offer 32-II.从上打下打印二叉树 easy

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {List<List<Integer>> list=new ArrayList<>();public List<List<Integer>> levelOrder(TreeNode root) {level(0,root);return list;}void level(int depth,TreeNode node){if(node==null) return;if(depth==list.size()){List<Integer> l=new ArrayList<>();list.add(l);}list.get(depth).add(node.val);depth++;level(depth,node.left);level(depth,node.right);}
}

跟上面那道题用的一个方法

剑指Offer 32-III.从上到下打印二叉树 medium

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {List<List<Integer>> list=new ArrayList<>();public List<List<Integer>> levelOrder(TreeNode root) {level(0,root);return list;}void level(int depth,TreeNode node){if(node==null) return;if(depth==list.size()){List<Integer> l=new ArrayList<>();list.add(l);}if(depth%2==0){list.get(depth).add(node.val);}else{list.get(depth).add(0,node.val);}level(depth+1,node.left);level(depth+1,node.right);}
}

跟上面一道题一样
List<Integer> 的一个方法重载 list.add(i,n)即在第i个元素插入n,list.add(n)是默认插到尾部,如果想插在头部,则是list.add(0,n)

剑指Offer 33.二叉搜索树的后序遍历数列 medium

class Solution {public boolean verifyPostorder(int[] postorder) {return recur(postorder,0,postorder.length-1);}boolean recur(int[] postorder,int i,int j){if(i>=j) return true;int m=i;while(postorder[m]<postorder[j]){m++;}int n=m;while(postorder[n]>postorder[j]){n++;}return n==j&&recur(postorder,i,m-1)&&recur(postorder,m,j-1);}
}

看答案觉得简单,一做就不会
使用的是分治法
因为后序树的特点是左子树都比最后一个数小,右子树都比最后一个数大
时间复杂度O(n^2),空间复杂度O(n)


http://www.ppmy.cn/news/2675.html

相关文章

【Pytorch】第 3 章 :进行数值估计的蒙特卡洛方法

&#x1f50e;大家好&#xff0c;我是Sonhhxg_柒&#xff0c;希望你看完之后&#xff0c;能对你有所帮助&#xff0c;不足请指正&#xff01;共同学习交流&#x1f50e; &#x1f4dd;个人主页&#xff0d;Sonhhxg_柒的博客_CSDN博客 &#x1f4c3; &#x1f381;欢迎各位→点赞…

解读小红书2022年母婴行业报告:心智种草的流量密码

母婴用户代际更迭&#xff0c;90后晋升为母婴消费主力军。新一代宝爸宝妈的关注点在哪里&#xff1f;品牌该如何通过小红书满足ta们的进阶需求&#xff0c;为母婴消费注入新活力&#xff1f; 本文将解读小红书官方发布的《2022年母婴行业人群洞察报告》&#xff0c;基于上千名用…

Linux 几种常见的自启动方式

1、rc.local 直接在/etc/rc.loacl文件中编辑需要开机自启动的脚本或程序&#xff0c;注意启动代码要放在exit之前&#xff0c;并且使用&符号后台执行&#xff0c;否则如果是死循环脚本或者程序的话就会卡住。因为是root权限启动的&#xff0c;所有执行权限比较高&#xff0…

【畅购商城】内网穿透之EchoSite

目录 概述 注册用户 抢注域名 ​​​​​​​下载客户端 ​​​​​​​编写配置文件 ​​​​​​​启动 ​​​​​​​访问 ​​​​​​​概述 EchoSite一款收费的内网映射工具&#xff08;已下架&#xff09; 花生壳&#xff1a;内网穿透工具&#xff0c;免费版…

js学习笔记

1.js代码要写在script标签中 <script type"text/javascript">for (let i0;i<5;i){document.write("<h1 stylecolor:red;>hello world</h1>")} </script>2.可以通过src的方式指定读取js文件进来&#xff0c;注意如果用这种方式…

Java#33(IO流)

目录 一.IO流 作用: (对于程序而言)用于读写数据(本地数据, 网络数据) 二.IO流体系 1.字节输出流 2.字节输入流 3.文件拷贝 3.字符集 字符流 字符输入流 字符输出流 缓冲流 转换流 序列化流 ​编辑反序列流 打印流 一.IO流 I: input O: output 流: 想流…

C#学习记录——软件工程师必备素养与技能

『聪明是一种天赋&#xff0c;而善良是一种选择。』—— 网络 1、软件工程师的基本素养 2、个人素质必修课程 3、项目开发流程 具备了良好的个人素质和基础的编程知识&#xff0c;作为一名优秀的开发人员&#xff0c;还应熟悉一个软件项目怎么开展工作&#xff0c;这就是项目…

C/C++入门002-C语言组成

文章目录1. C工程创建1.1 基于Code::Blocks创建工程1.2 Code::Blocks界面设置2. C语言程序组成2.1函数2.1.1 主函数2.1.2 其它函数2.1.3 如何执行定义好的函数2.2 输出函数printf2.2.1 编译输出为exe可执行文件2.3 C语言要求2.3.1 注释2.4 C语言程序练习2.4.1 输出三角形代码1&…