当前位置: 首页 > news >正文

aliyun怎么建网站关键词seo公司推荐

aliyun怎么建网站,关键词seo公司推荐,做网站优势,wordpress+主题+欣赏目录 题目1- 思路2- 实现⭐103. 二叉树的锯齿形层序遍历——题解思路 2- ACM实现 题目 原题连接:103. 二叉树的锯齿形层序遍历 1- 思路 二叉树的层序遍历,遇到奇数时,利用 Collections.reverse() 翻转即可 2- 实现 ⭐103. 二叉树的锯齿形层…

目录

  • 题目
  • 1- 思路
  • 2- 实现
    • ⭐103. 二叉树的锯齿形层序遍历——题解思路
  • 2- ACM实现


题目

  • 原题连接:103. 二叉树的锯齿形层序遍历

1- 思路

  • 二叉树的层序遍历,遇到奇数时,利用 Collections.reverse() 翻转即可

2- 实现

⭐103. 二叉树的锯齿形层序遍历——题解思路

在这里插入图片描述

class Solution {public List<List<Integer>> res = new ArrayList<>();public List<List<Integer>> zigzagLevelOrder(TreeNode root) {return Traversal(root);}public List<List<Integer>> Traversal(TreeNode root){if(root==null){return res;}// 借助 queueQueue<TreeNode> queue = new LinkedList<>();queue.offer(root);// queue 不空int count = 0;while(!queue.isEmpty()){int len = queue.size();List<Integer> path = new ArrayList<>();while(len>0){TreeNode node = queue.poll();path.add(node.val);if(node.left!=null){queue.offer(node.left);}if(node.right!=null){queue.offer(node.right);}len--;}count++;if(count%2==1){res.add(new ArrayList(path));}else{Collections.reverse(path);res.add(new ArrayList(path));}}return res;}
}

2- ACM实现

public class levelTraversal {static class TreeNode{int val;TreeNode left;TreeNode right;TreeNode(){}TreeNode(int x){val = x;}}public static TreeNode build(Integer[] nums){Queue<TreeNode> queue = new LinkedList<>();TreeNode root = new TreeNode(nums[0]);queue.offer(root);int index = 1;while(!queue.isEmpty() && index<nums.length){TreeNode node = queue.poll();if(nums[index]!=null && index<nums.length){node.left = new TreeNode(nums[index]);queue.offer(node.left);}index++;if(nums[index]!=null && index<nums.length){node.right = new TreeNode(nums[index]);queue.offer(node.right);}index++;}return root;}static List<List<Integer>> res =new ArrayList<>();public static List<List<Integer>> levelTraversal(TreeNode root){if(root==null) {return res;}Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);int level = 0;while(!queue.isEmpty()){List<Integer> iterm = new ArrayList<>();int len = queue.size();while(len>0){TreeNode node = queue.poll();iterm.add(node.val);if(node.left!=null){queue.offer(node.left);}if(node.right!=null){queue.offer(node.right);}len--;}if(level%2==1) {Collections.reverse(iterm);}res.add(new ArrayList<>(iterm));}return res;}public static void main(String[] args) {Scanner sc = new Scanner(System.in);String input = sc.nextLine();input = input.replace("[","");input = input.replace("]","");String[] parts = input.split(",");Integer[] nums = new Integer[parts.length];for(int i = 0 ; i < parts.length ;i++){if(!parts[i].equals("null")){nums[i] = Integer.parseInt(parts[i]);}else{nums[i] = null;}}TreeNode root = build(nums);levelTraversal(root);System.out.println("结果为"+res.toString());}
}
http://www.hkea.cn/news/408614/

相关文章:

  • 创建网站开发公司互联网推广引流是做什么的
  • 万盛集团网站建设seo网站推广全程实例
  • 做教育的网站需要资质吗网站怎么开发
  • 微网站怎么做滚动中国万网域名注册官网
  • 个人如何免费建网站seo在线优化工具 si
  • 双线主机可以做彩票网站吗网络推广合作协议
  • 做外贸的b2b网站域名批量查询系统
  • 建设网站需要哪些职位网站建设策划书
  • 苏州网站建设哪里好网站点击排名优化
  • 网站建设收费标准策划百度推广关键词越多越好吗
  • 网站怎么做更新吗如何建立网页
  • 国外建设工程招聘信息网站tool站长工具
  • 专业做相册书的网站电商网站建设制作
  • 银川网站开发公司电话东莞网
  • 环境保护局网站管理制度建设百度指数的主要功能有
  • 安装wordpress提示500错误关键词优化的策略有哪些
  • 企业网站建设公司排名深圳高端seo公司助力企业
  • 做网站套餐网站seo
  • 网站上的代码网页怎么做的下载百度软件
  • 网站功能模块建设搜狗推广
  • 网站做推广有用吗网站页面设计
  • 做简报的网站广州搜发网络科技有限公司
  • 南乐县住房和城乡建设局网站制作网站的步骤是什么
  • 金华做网站最专业的公司搜易网提供的技术服务
  • wordpress适合门户网站吗怎么营销自己的产品
  • 常用的网站类型有哪些seo优化专员编辑
  • 网站专题框架怎么做海阳seo排名
  • 手机网站代码下载黄页网站推广服务
  • 做网站前端多少钱在线bt种子
  • wordpress+模版+推荐专业网站seo推广