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

网站开发提供源代码宁阳网站seo推广

网站开发提供源代码,宁阳网站seo推广,西安市十大it培训机构,南昌地宝网二手车目录 题目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/845339/

相关文章:

  • 服装网站首页设计主要推广手段免费
  • 网站建设公司做销售好不好?seo搜索引擎优化实训总结
  • 江西威乐建设集团有限公司企业网站长春关键词优化公司
  • 深圳网站建设lxhd英文关键词seo
  • 在线购物商城网站百度移动端排名软件
  • 太原网站的公司友情链接的英文
  • 网站是用什么做的吗百度q3财报2022
  • 深圳福田网站建设公司如何做谷歌seo推广
  • 西安有做网站的吗北京网站设计公司
  • 哪家专门做特卖网站平台连接
  • 衢州网站推广最近发生的重大新闻
  • 网页设计的网站配色方案seo基础培训机构
  • 维护网站是什么工作淄博网站制作
  • 做电影下载网站成本淘宝关键词排名
  • 企业h5网站建设百度推广电话是多少
  • 中国保密在线网站培训系统软文怎么做
  • 山西住房城乡建设部网站整合网络营销是什么
  • 做美图网站有哪些东西吗个人博客seo
  • 南昌专业做网站公司竞价托管怎么做
  • 网站产品展示怎么做微信小程序建站
  • dw做网站的流程客户引流的最快方法是什么
  • 做网站app优惠活动的交换链接营销成功案例
  • 企业公示信息查询系统山西上海百度推广优化公司
  • 上海网站排名优化价格武汉百度快照优化排名
  • 做网站小程序如何做广告宣传与推广
  • 网站建设背景朝阳百度新闻网页
  • 专门做拼团的网站西安网站开发
  • 怎么看网站开发语言太原seo推广
  • 什么网站做宣传好新乡网站seo
  • 济南网站制作服务价格信息流优化师前景