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

防城港市网站建设_网站建设公司_VS Code_seo优化

上传的网站打不开,搜索推广平台有哪些,淘宝官网首页登录账号,wordpress微信登录设置从上往下打印二叉树的每个节点#xff0c;同一层的节点按照从左到右的顺序打印。例如输入下图的二叉树#xff0c;则一次打印出8#xff0c;6#xff0c;10#xff0c;5#xff0c;7#xff0c;9#xff0c;11。 思路#xff1a;利用队列,将左右子树加入队列末尾同一层的节点按照从左到右的顺序打印。例如输入下图的二叉树则一次打印出861057911。 思路利用队列,将左右子树加入队列末尾取出结点 代码 package offer; import java.util.LinkedList; import java.util.Queue; class BineryTree {     int val;     BineryTree left null;     BineryTree right null;     BineryTree(int val)     {         this.val val;     } } public class ti32 {     static void BineryTreeToQueue(BineryTree head)     {         if(headnull)         {             return;         }         QueueBineryTree queue new LinkedListBineryTree();         queue.add(head);         while(!queue.isEmpty())         {             BineryTree x queue.poll();             System.out.println(x.val);             if(x.left!null)             {                 queue.add(x.left);             }             if(x.right!null)             {                 queue.add(x.right);             }         }     }     public static void main(String[] args)     {         BineryTree a new BineryTree(8);         BineryTree b new BineryTree(6);         BineryTree c new BineryTree(10);         BineryTree d new BineryTree(5);         BineryTree e new BineryTree(7);         BineryTree f new BineryTree(9);         BineryTree g new BineryTree(11);         a.left b;         a.right c;         b.left d;         b.right e;         c.left f;         c.right g;         BineryTreeToQueue(a);     } }题目三 请实现一个函数按照之字形打印二叉树即第一行按照从左到右的顺序打印第二层按照从右至左的顺序打印第三行按照从左到右的顺序打印其他行以此类推。 代码 package offer; import java.util.ArrayList; import java.util.Stack; class BineryTree {     int val;     BineryTree left null;     BineryTree right null;     BineryTree(int val)     {         this.val val;     } } public class ti32 {     static ArrayListArrayListInteger BineryTreeToQueue(BineryTree head)     {         ArrayListArrayListInteger alist new ArrayList();                   if(headnull)         {             return alist;         }         StackBineryTree stack1 new StackBineryTree();         StackBineryTree stack2 new StackBineryTree();         stack1.add(head);         while(!stack1.empty()||!stack2.empty())         {             ArrayListInteger list new ArrayList();                 if(!stack1.empty())                 {                     while(!stack1.empty())                     {                         BineryTree x stack1.pop();                         list.add(x.val);                         if(x.left!null)                         {                             stack2.add(x.left);                         }                         if(x.right!null)                         {                             stack2.add(x.right);                         }                     }                                      }                 else                 {                     while(!stack2.empty())                     {                         BineryTree x stack2.pop();                         list.add(x.val);                         if(x.right!null)                         {                             stack1.add(x.right);                         }                         if(x.left!null)                         {                             stack1.add(x.left);                         }                     }                                      }                 alist.add(list);             }         return alist;         }          public static void main(String[] args)     {         BineryTree a new BineryTree(8);         BineryTree b new BineryTree(6);         BineryTree c new BineryTree(10);         BineryTree d new BineryTree(5);         BineryTree e new BineryTree(7);         BineryTree f new BineryTree(9);         BineryTree g new BineryTree(11);         a.left b;         a.right c;         b.left d;         b.right e;         c.left f;         c.right g;         ArrayListArrayListInteger alist BineryTreeToQueue(a);         for(int i0;ialist.size();i)         {             for(int j0;jalist.get(i).size();j)             {                 System.out.print(alist.get(i).get(j) );             }             System.out.println();         }     } }
http://www.ihoyoo.com/news/72182.html

相关文章:

  • 长沙手机网站建设哪些石家庄做商城网站的公司
  • 网站开发用户需求说明书网站备案接入商名称
  • 苏州公司网站设计普宁17网站一起做网店
  • 服务网站备案在360网站上怎么做推广
  • 外贸网站平台都有哪些平台威海团购网站建设
  • 做网站应该掌握的技术吴中seo页面优化推广
  • 网站源码模板南京口碑最好的装修公司
  • 做静态网站成本沈阳定制网络机箱机柜
  • 做外贸必须建网站吗手机浏览器
  • 怎样做信息收费网站百度会员
  • 做win精简系统的网站东吴钢结构网架公司
  • 网站建设及制作教程天堂2免费服务器
  • 哪个网站做的最好小程序ui界面设计案例
  • 网站架构软件用户体验设计原则
  • h5制作的炫酷个人网站汕头网站模板价格
  • wordpress修改网站名称小红书推广方法
  • 2015年做哪些网站致富网站免费正能量直接进入小说
  • 创建私人网站wordpress怎么修改图片大小
  • 公司做网站的申请无锡网站建设专注千客云网络
  • 国内的优秀网站WordPress外链自动转内链
  • 网站的风格有哪些网站添加js广告位
  • 高效网站建设企业响应网站
  • 免费的海报设计网站芸志建站怎么建立网站
  • 晋城市住建设局网站泰州网站制作报价
  • 免费头像生成制作网站展览网站源码
  • 网站制作有哪些企业商城分销
  • 梧州建设厅官方网站百度网站推广咨询
  • app科技网站建设用ps怎么做网站导航条
  • 张家港快速网站建设58长春招聘网
  • 免费做企业网站菏泽住房和城乡建设局网站