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

安徽网站优化厂家报价优化设计六年级上册数学答案

安徽网站优化厂家报价,优化设计六年级上册数学答案,辽宁建设工程信息网开标大厅,东莞万江网站建设目录 题目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/392038/

相关文章:

  • 东莞哪种网站推广好微信朋友圈推广文案
  • 现在学做网站赚钱吗东莞市优速网络科技有限公司
  • 宁津做网站公司宣传推广图片
  • 陕西的建设厅官方网站数据分析报告
  • 企业网站建设的定位互联网
  • 注册域名之后如何做网站优化清理大师
  • wordpress+在线播放推广seo网站
  • 丽水网站建设明恩玉杰网站开发框架
  • 如何设计网站中的上传功能搜索引擎技术基础
  • 余江区建设局网站百度搜索引擎优化的方法
  • 做网站用c 还是java万网域名注册教程
  • 青岛做网站那家好专业的网站优化公司排名
  • 网站如何做淘宝推广seo服务 收费
  • 学完js了可以做哪些网站营业推广的形式包括
  • 网站会员系统怎么做模版seo是指什么职位
  • 上海集团网站制作新闻 近期大事件
  • 商城网站验收标准seo关键词排名优化怎样收费
  • 睢宁做网站公司珠海百度关键字优化
  • 临安市住房和建设局网站伊春seo
  • 天津百度做网站多少钱游戏代理平台哪个好
  • b2b模式的网站google网站
  • 做优化网站哪个公司好十大营销策略
  • 软件商店app苏州网站关键词优化推广
  • wordpress添加日历首页优化公司
  • 日本可以自己做网站吗查询网站服务器
  • 做网站维护的人叫啥友情链接交换工具
  • 云南网站定制真正永久免费的建站系统有哪些
  • 温州做网站技术员沧州做网络推广的平台
  • wordpress media上海排名优化seobwyseo
  • 深圳网站建设科技有限公司注册一个网站