剑指offer:31-32记录(4道)

举报
兔老大 发表于 2021/04/21 22:32:57 2021/04/21
【摘要】 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。   示例 1: 输入:pushed = [1,2,3,4,5], po...

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {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 的排列。

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


  
  1. class Solution {
  2. public boolean validateStackSequences(int[] pushed, int[] popped) {
  3. Stack<Integer> stack = new Stack<Integer>();
  4. int j = 0;
  5. for(int i = 0;i < pushed.length;i++){
  6. stack.push(pushed[i]);
  7. while(!stack.empty() && stack.peek() == popped[j]){
  8. stack.pop();
  9. j++;
  10. }
  11. }
  12. return stack.empty();
  13. }
  14. }

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

 

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

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

[3,9,20,15,7]
 

提示:

节点总数 <= 1000

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


  
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public int[] levelOrder(TreeNode root) {
  12. if (root == null) return new int[0];
  13. Queue<TreeNode> queue = new LinkedList<>();
  14. List<Integer> res = new ArrayList<>();
  15. queue.add(root);
  16. while(!queue.isEmpty()) {
  17. TreeNode node = queue.poll();
  18. res.add(node.val);
  19. if(node.left != null) queue.add(node.left);
  20. if(node.right != null) queue.add(node.right);
  21. }
  22. int[] _result = new int[res.size()];
  23. for (int i = 0; i < res.size(); i++) {
  24. _result[i] = res.get(i);
  25. }
  26. return _result;
  27. }
  28. }

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

 

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

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

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

提示:

节点总数 <= 1000

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


  
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public List<List<Integer>> levelOrder(TreeNode root) {
  12. Queue<TreeNode> queue = new LinkedList<>();
  13. List<List<Integer>> res = new ArrayList<>();
  14. if(root != null) queue.add(root);
  15. while(!queue.isEmpty()) {
  16. List<Integer> tmp = new ArrayList<>();
  17. for(int i = queue.size(); i > 0; i--) {
  18. TreeNode node = queue.poll();
  19. tmp.add(node.val);
  20. if(node.left != null) queue.add(node.left);
  21. if(node.right != null) queue.add(node.right);
  22. }
  23. res.add(tmp);
  24. }
  25. return res;
  26. }
  27. }

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

 

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

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

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

提示:

节点总数 <= 1000

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


  
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public List<List<Integer>> levelOrder(TreeNode root) {
  12. Queue<TreeNode> queue = new LinkedList<>();
  13. List<List<Integer>> res = new ArrayList<>();
  14. if(root != null) queue.add(root);
  15. while(!queue.isEmpty()) {
  16. List<Integer> tmp = new ArrayList<>();
  17. for(int i = queue.size(); i > 0; i--) {
  18. TreeNode node = queue.poll();
  19. tmp.add(node.val);
  20. if(node.left != null) queue.add(node.left);
  21. if(node.right != null) queue.add(node.right);
  22. }
  23. if(res.size() % 2 == 1) Collections.reverse(tmp);
  24. res.add(tmp);
  25. }
  26. return res;
  27. }
  28. }

 

文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。

原文链接:fantianzuo.blog.csdn.net/article/details/104763786

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。