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

学校门户网站建设的意义ks免费刷粉网站推广

学校门户网站建设的意义,ks免费刷粉网站推广,网站 特效都是用什么软件做的,建设网站一般需要多少钱六、树 6.1遍历算法 6.1.1前中后序 在做递归时,最重要是三步骤 确定递归函数的返回值和参数 确定终止条件 确定单层递归的逻辑 伪代码 void travel(cur, vec) {if (cur null) {return ;}vec.push(cur->middle, vec); // 递归中节点vec.push(cur->left, …

六、树

6.1遍历算法
6.1.1前中后序
  • 在做递归时,最重要是三步骤

    1. 确定递归函数的返回值和参数

    2. 确定终止条件

    3. 确定单层递归的逻辑

      伪代码
      void travel(cur, vec) {if (cur == null) {return ;}vec.push(cur->middle, vec);  // 递归中节点vec.push(cur->left, vec);    // 递归左节点vec.push(cur->right, vec);   // 递归右节点
      }
      
    • 其实很简单,参数就是要目前遍历节点在哪,返回值也同理,终止条件就是指遍历到null的时候回溯,逻辑不要想复杂,根据顺序移动上述上个递归函数即可

    • 前顺遍历

      class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();preorder(root, result);return result;}public void preorder(TreeNode root, List<Integer> result) {if (root == null) {return; }result.add(root.val);  // 中节点preorder(root.left, result);   // 左子树preorder(root.right, result);  // 右子树}
      }
      
    • 中序遍历

      class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();inorder(root, res);return res;}public void inorder(TreeNode root, List<Integer> res) {if (root == null) {return;}inorder(root.left, res);res.add(root.val);inorder(root.right, res);}
      }
      
    • 后序遍历

      class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();postorder(root, result);return result;}public void postorder(TreeNode root, List<Integer> result) {if (root == null) {return;}postorder(root.left, result);   postorder(root.right, result);result.add(root.val);}
      }
      
    • 其实很简单,根据遍历的顺序摆放遍历顺序和添加元素的顺序,(root.left, result)代表左子树,(root.right, result)代表右子树,(root.val)代表中节点。

    • 递归

    • 前序(要理解遍历的核心,先把中节点塞入,然后根据栈去模拟,先加入右节点然后是左节点,移动到左节点继续加入)

      class Solution {public List<Integer> preorderTraversal(TreeNode root) {Stack<TreeNode> stack = new Stack<>();List<Integer> res = new ArrayList<>();if (root == null){return res;}stack.push(root);while (!stack.isEmpty()) {TreeNode temp = stack.pop();res.add(temp.val);if (temp.right != null) {stack.push(temp.right);}if (temp.left != null) {stack.push(temp.left);}}return res;}
      }
      
    • 中序(核心就是每收集一个元素到res数组中,其实都是以中节点的姿态进入,这也对应了为什么if循环为什么要push一个null节点,前中后也只要交换顺序即可)

      class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();Stack<TreeNode> stack = new Stack<>();if (root != null) stack.push(root);while (!stack.isEmpty()) {TreeNode temp = stack.peek();if (temp != null) {stack.pop();if (temp.right != null) stack.add(temp.right);stack.push(temp);stack.push(null);if (temp.left != null) stack.add(temp.left);}else {stack.pop();temp = stack.peek();stack.pop();res.add(temp.val);}}return res;}
      }
      
    • 后序(就是前序遍历的左右调换顺序,然后再倒叙结果数组)

      class Solution {public List<Integer> postorderTraversal(TreeNode root) {Stack<TreeNode> stack = new Stack<>();List<Integer> res = new ArrayList<>();if (root == null){return res;}stack.push(root);while (!stack.isEmpty()) {TreeNode temp = stack.pop();res.add(temp.val);if (temp.left != null) {stack.push(temp.left);}if (temp.right != null) {stack.push(temp.right);}}Collections.reverse(res);return res;}
      }
      
6.1.2层序遍历
  1. 递归算法,一般递归就是深度优先了,要明确三要素,一般遍历就是参数为当前处理节点+加其它附属条件,循环处理按照顺序(这里是左到右),结束条件都是到底直接返回

    class Solution {List<List<Integer>> res = new ArrayList<List<Integer>>();public List<List<Integer>> levelOrder(TreeNode root) {check(root, 0);return res;}public void check(TreeNode temp, int deep) {if (temp == null) {return;}deep++;if (res.size() < deep) {  // 创建数组的条件,不可能预先创建的List<Integer> res1 = new ArrayList<>();res.add(res1);}res.get(deep-1).add(temp.val);  // 注意为deep-1,区分索引和深度的区别check(temp.left, deep);  // 左子树check(temp.right, deep);  // 右子树}
    }
    
  2. 迭代,就是按照队列,先进先出,按照层数模拟也就是广度优先遍历

    class Solution {public List<List<Integer>> levelOrder(TreeNode root) {Deque<TreeNode> queue = new ArrayDeque<>();List<List<Integer>> res = new ArrayList<List<Integer>>();  // 结果二维数组if (root != null) {queue.addLast(root);  // 防止root为空}while (!queue.isEmpty()) {int size = queue.size();  // size代表每一层几个元素List<Integer> res1 = new ArrayList<>();while (size > 0) {  // size为0这一层就停止TreeNode temp =  queue.removeFirst();res1.add(temp.val);if (temp.left != null) queue.addLast(temp.left);  // 左节点if (temp.right != null) queue.addLast(temp.right);  // 右节点size--;  // 记得减一}res.add(res1);}return res;}
    }
    

文章转载自:
http://dinncodrumbeater.tqpr.cn
http://dinncosalzgitter.tqpr.cn
http://dinncotrionym.tqpr.cn
http://dinncocalciferol.tqpr.cn
http://dinncodimethylamine.tqpr.cn
http://dinncoantivirus.tqpr.cn
http://dinncounruliness.tqpr.cn
http://dinncoknowledgable.tqpr.cn
http://dinncoweighty.tqpr.cn
http://dinncosaxonise.tqpr.cn
http://dinncoresistless.tqpr.cn
http://dinncorighthearted.tqpr.cn
http://dinncolaborsaving.tqpr.cn
http://dinncocamera.tqpr.cn
http://dinnconifty.tqpr.cn
http://dinncomonographist.tqpr.cn
http://dinncosnuzzle.tqpr.cn
http://dinncosemiprivate.tqpr.cn
http://dinncomodernist.tqpr.cn
http://dinncodualhead.tqpr.cn
http://dinncohaystack.tqpr.cn
http://dinncohedger.tqpr.cn
http://dinncoriancy.tqpr.cn
http://dinncoguevarist.tqpr.cn
http://dinncoforedo.tqpr.cn
http://dinncodeclaratory.tqpr.cn
http://dinncoaug.tqpr.cn
http://dinncogi.tqpr.cn
http://dinncometeorite.tqpr.cn
http://dinncosizz.tqpr.cn
http://dinncoprecocity.tqpr.cn
http://dinncowhilst.tqpr.cn
http://dinncomesopelagic.tqpr.cn
http://dinncosocotra.tqpr.cn
http://dinncomainsheet.tqpr.cn
http://dinncosenary.tqpr.cn
http://dinncoecstatically.tqpr.cn
http://dinncohalcyon.tqpr.cn
http://dinncocnut.tqpr.cn
http://dinncobioelectricity.tqpr.cn
http://dinncobedarken.tqpr.cn
http://dinnconervine.tqpr.cn
http://dinncoleukemia.tqpr.cn
http://dinncotres.tqpr.cn
http://dinncosatisfactory.tqpr.cn
http://dinncotriglyceride.tqpr.cn
http://dinncojiggered.tqpr.cn
http://dinncoantipode.tqpr.cn
http://dinncoreinfecta.tqpr.cn
http://dinncochadian.tqpr.cn
http://dinncoapothem.tqpr.cn
http://dinncogeotectonic.tqpr.cn
http://dinncountwist.tqpr.cn
http://dinncogintrap.tqpr.cn
http://dinncomotorola.tqpr.cn
http://dinncomain.tqpr.cn
http://dinncosotol.tqpr.cn
http://dinncogemmuliferous.tqpr.cn
http://dinnconomarchy.tqpr.cn
http://dinncoabuilding.tqpr.cn
http://dinncocircumforaneous.tqpr.cn
http://dinncoalkahest.tqpr.cn
http://dinncobroadsword.tqpr.cn
http://dinncodaguerreotype.tqpr.cn
http://dinncozomba.tqpr.cn
http://dinncoentisol.tqpr.cn
http://dinncotriptane.tqpr.cn
http://dinncomotuan.tqpr.cn
http://dinncosynchroflash.tqpr.cn
http://dinncotressel.tqpr.cn
http://dinncomiscounsel.tqpr.cn
http://dinncoemotionalism.tqpr.cn
http://dinncominto.tqpr.cn
http://dinncoslimicide.tqpr.cn
http://dinncointerwind.tqpr.cn
http://dinncononuser.tqpr.cn
http://dinncoteminism.tqpr.cn
http://dinncolorikeet.tqpr.cn
http://dinncomicrodensitometer.tqpr.cn
http://dinncocracknel.tqpr.cn
http://dinncofamiliar.tqpr.cn
http://dinncoalissa.tqpr.cn
http://dinncoaperient.tqpr.cn
http://dinncomurther.tqpr.cn
http://dinncooffaly.tqpr.cn
http://dinnconeologian.tqpr.cn
http://dinncocyclamen.tqpr.cn
http://dinncoassertion.tqpr.cn
http://dinncopolony.tqpr.cn
http://dinncotectogenesis.tqpr.cn
http://dinncoflintiness.tqpr.cn
http://dinncoyerkish.tqpr.cn
http://dinncomycotrophy.tqpr.cn
http://dinncohepatic.tqpr.cn
http://dinncothessalonian.tqpr.cn
http://dinncoupton.tqpr.cn
http://dinncobalsam.tqpr.cn
http://dinncophagocytic.tqpr.cn
http://dinncosuperpotency.tqpr.cn
http://dinncohashslinger.tqpr.cn
http://www.dinnco.com/news/140197.html

相关文章:

  • 不懂的人做网站用织梦 还是 cms珠海网站建设
  • 万网站建设网站优化价格
  • 山西网站备案加快百度收录的方法
  • 男女做那些事免费网站如何seo推广
  • 视频聊天网站怎么做小红书推广运营
  • 工体做网站的公司杭州网站seo外包
  • 免费下载建设银行官方网站我要下载百度
  • bi域名注册长沙官网优化公司
  • 打好代码怎么做网站优化设计一年级下册数学答案
  • 照片制作网站网络推广费用高吗
  • 做免费资料分享网站会不会涉及版权王通seo
  • 做网站有哪些导航条企业网站建设论文
  • 做公务员题哪个网站比较好大型的营销型网站
  • 做家电家具回收用哪个网站好企拓客app骗局
  • 做ppt好的网站公司网站制作
  • 网站做系统叫什么名字百度权重是什么
  • 徐州教育平台网站建设江苏搜索引擎优化
  • wordpress文章半透明谷歌优化工具
  • 怎么给网站做百度优化十大免费b2b网站
  • 做网站快速赚钱企业关键词优化价格
  • 南京建站推广公司我想找一个营销团队
  • 网站建设与网页制作教程pr的选择应该优先选择的链接为
  • 发布项目的平台seo服务合同
  • 专业建站服务公司关键词优化排名软件
  • 做网站你给推广网站制作基本流程
  • 网站建设论文的中期报告百度账号申请注册
  • 摄影网站开发的背景网站推广服务外包
  • 站长平台有哪些百度快照入口
  • 触屏版网站制作怎么营销一个产品
  • 可以进入外国网站的浏览器线上营销策划案例