输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。

示例 1:

输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
示例 2:

输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出:false
解释:1 不能在 2 之前弹出。

提示:

0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed 是 popped 的排列。

思路:模拟,每压入一个就尝试弹出到不能再弹。到最后栈空就可以。

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

从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。

例如:
给定二叉树: [3,9,20,null,null,15,7],

3
   / \
  9  20
    /  \
   15   7
返回:

[3,9,20,15,7]

提示:

节点总数 <= 1000

思路:层序遍历,出队列时用另一个list记录,最后转为数组即可。

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public int[] levelOrder(TreeNode root) {if (root == null) return new int[0];Queue<TreeNode> queue = new LinkedList<>();List<Integer> res = new ArrayList<>();queue.add(root);while(!queue.isEmpty()) {TreeNode node = queue.poll();res.add(node.val);if(node.left != null) queue.add(node.left);if(node.right != null) queue.add(node.right);}int[] _result = new int[res.size()];for (int i = 0; i < res.size(); i++) {_result[i] = res.get(i);}return _result;}
}

从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。

例如:
给定二叉树: [3,9,20,null,null,15,7],

3
   / \
  9  20
    /  \
   15   7
返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

提示:

节点总数 <= 1000

思路:遍历时一次弹完一层所有节点(队列内当时的所有节点),并用一个list保存。

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public List<List<Integer>> levelOrder(TreeNode root) {Queue<TreeNode> queue = new LinkedList<>();List<List<Integer>> res = new ArrayList<>();if(root != null) queue.add(root);while(!queue.isEmpty()) {List<Integer> tmp = new ArrayList<>();for(int i = queue.size(); i > 0; i--) {TreeNode node = queue.poll();tmp.add(node.val);if(node.left != null) queue.add(node.left);if(node.right != null) queue.add(node.right);}res.add(tmp);}return res;}
}

请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。

例如:
给定二叉树: [3,9,20,null,null,15,7],

3
   / \
  9  20
    /  \
   15   7
返回其层次遍历结果:

[
  [3],
  [20,9],
  [15,7]
]

提示:

节点总数 <= 1000

思路:和上题一样,只不过加一句话,翻转部分temp。

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public List<List<Integer>> levelOrder(TreeNode root) {Queue<TreeNode> queue = new LinkedList<>();List<List<Integer>> res = new ArrayList<>();if(root != null) queue.add(root);while(!queue.isEmpty()) {List<Integer> tmp = new ArrayList<>();for(int i = queue.size(); i > 0; i--) {TreeNode node = queue.poll();tmp.add(node.val);if(node.left != null) queue.add(node.left);if(node.right != null) queue.add(node.right);}if(res.size() % 2 == 1) Collections.reverse(tmp);res.add(tmp);}return res;}
}

剑指offer:31-32记录(4道)相关推荐

  1. 剑指offer做题记录

    1. 剑指 Offer 03. 数组中重复的数字   力扣 class Solution { public:int findRepeatNumber(vector<int>& nu ...

  2. 剑指offer刷题记录 python3 Java

    剑指offer刷题记录 python3 Java 剑指 Offer 09. 用两个栈实现队列 剑指 Offer 10- I. 斐波那契数列 剑指 Offer 03. 数组中重复的数字 [★]剑指 Of ...

  3. 【LeetCode】剑指 Offer 31. 栈的压入、弹出序列

    [LeetCode]剑指 Offer 31. 栈的压入.弹出序列 文章目录 [LeetCode]剑指 Offer 31. 栈的压入.弹出序列 package offer;import java.uti ...

  4. 剑指offer 面试32题

    面试32题: 题目:从上到下打印二叉树 题:不分行从上到下打印二叉树 解题代码: # -*- coding:utf-8 -*- # class TreeNode: # def __init__(sel ...

  5. 剑指Offer 刷题记录

    文章目录 剑指offer题目 01. 二维数组中的查找 02. 替换空格 03. 从尾到头打印链表 04. 重建二叉树 05. 两个堆栈模拟一个队列 06. 旋转数组的最小数字 07. 斐波那契数列 ...

  6. 剑指offer刷题记录(上)

    记录刷题过程,方便自己后期回顾 题目来自<剑指offer>,在牛客上OC,思路大多来自剑指offer,偶尔来自自己的碎碎念,代码自己瞎写的,如果有更优的方法请告诉我,谢谢大佬们 语言:py ...

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

    声明:本系列博客是对何海涛<剑指offer>的关键点总结. 1.问题描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序. 如第一个输入序列为{1 2 ...

  8. 剑指offer:12-17记录

    请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左.右.上.下移动一格.如果一条路径经过了矩阵的某一格,那么该路径不能再 ...

  9. 剑指offer:63-66记录

    假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少? 示例 1: 输入: [7,1,5,3,6,4] 输出: 5 解释: 在第 2 天(股票价格 = 1)的时候 ...

最新文章

  1. Mysql字符串字段判断是否包含某个字符串的3种方法
  2. mac connect to host localhost port 22: Connection refused
  3. php购物系统论文答辩老师评价,答辩指导教师的评语大全
  4. C++ 静态链表(用数组模拟动态链表)
  5. MyBatis中ThreadLocal
  6. Android常用的一些make命令(转载)--不错
  7. 2020年,图机器学习将走向何方?
  8. idea里边创建类的时候和方法自动生成注释
  9. 微服务之旅的经验分享
  10. centos查询是否有安装软件包
  11. 使用pyinstaller打包pyqt5出错问题解决
  12. java调用Dos命令
  13. 【EASYDOM系列教程】之属性操作
  14. simscape multibody仿真报错装配位置_大型结构件虚拟装配仿真软件,三维数字化分析,现场直接调整...
  15. python颜色参数_python matplotlib:plt.scatter() 大小和颜色参数详解
  16. if语句 power query_判断(if)语句
  17. 【编程入门】密码破译
  18. 外卖cps返利定制开发源码平台小程序美团饿了么红包电影票券分销
  19. Sequoia(基于JDBC的数据库集群中间件)用户手册
  20. 制作openstack镜像win7.qcow2(centos/ubuntu/win镜像分享)

热门文章

  1. matlab 条形图横坐标,Matlab条形图bar横坐标间距设置
  2. 中断请求request_irq
  3. 哪些设计模式最值得学习
  4. android unzip file,Unzip File in Android Assets
  5. activity 点击后传递数据给fragment_Fragment 新特性 : Fragment Result API 使用以及源码分析
  6. python实验四_python实验四
  7. 准确检测图像的轮廓 opencv_图像处理案例实战
  8. LSGO软件技术团队2015~2016学年第六周(1005~1011)总结
  9. 【转】matlab与C/C++混合编程——在Windows/Linux上调用Matlab编译的动态库文件
  10. 【转】四、关于如何使用软件库的一个技巧-避免重复配置属性表