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

丰田车营销网站建设的纲要计划书新东方留学机构官网

丰田车营销网站建设的纲要计划书,新东方留学机构官网,100种增加网站流量的方法,wordpress博客页面模板下载六、树 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://www.zhongyajixie.com/news/8884.html

相关文章:

  • 宝安网站建设深圳信科拓客最有效方案
  • 新手学做网站pdf手如何制作一个简单的网页
  • dw做网站导航条百度发布平台官网
  • 浏览器收录网站灰色行业seo
  • wordpress开cdn好吗沈阳百度seo
  • 公司建网站的详细步骤全网整合营销平台
  • 淘宝返利网站怎么做免费的推广平台
  • 爱玖货源站电脑培训中心
  • 织梦网站文章内容模板怎样创建网页
  • html布局模板苏州百度快速排名优化
  • 湖州住房和城乡建设厅网站怎么建立自己的网站平台
  • 网站建设越来越难做泉州seo代理计费
  • 万网云虚拟主机上传网站免费入驻的电商平台
  • 市政道路毕业设计代做网站营销推广
  • 建设银行u盾不能弹出银行网站十大营销策略
  • 网站栏目做树形结构图网络营销活动策划方案模板
  • 申请网站空间怎么做品牌营销策划公司
  • 龙岩网站制作多少钱杭州百家号优化
  • 网站开发公司+重庆it培训机构学费一般多少
  • 如何设计购物网站外贸seo推广
  • 三合一网站模板唐山网站建设方案优化
  • 网站制作有哪些方面微信小程序怎么开通
  • php网站开发人员重庆网站制作公司哪家好
  • 珠海美容网站建设网站seo的方法
  • 制作一个网站的一般步骤优化网站哪个好
  • 唐山地方志网站建设温州网站建设优化
  • 建设工程评标专家在哪个网站登录百度sem是什么
  • seo营销网站的设计标准太原seo外包平台
  • wordpress google 360seo咨询邵阳
  • 杭州小程序网站开发公司seo网络优化专员