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

wordpress采集文章系统优化开关在哪里

wordpress采集文章,系统优化开关在哪里,新网站怎么快速收录必做,视频门户网站建设方案专栏#xff1a;Java数据结构秘籍 个人主页#xff1a;手握风云 目录 一、非递归实现遍历二叉树 1.1. 二叉树的前序遍历 1.2. 二叉树的中序遍历 1.3. 二叉树的后序遍历 一、非递归实现遍历二叉树 1.1. 二叉树的前序遍历 我们这里要使用栈来进行实现。我们反向思考一下为… 专栏Java数据结构秘籍 个人主页手握风云 目录 一、非递归实现遍历二叉树 1.1. 二叉树的前序遍历 1.2. 二叉树的中序遍历 1.3. 二叉树的后序遍历 一、非递归实现遍历二叉树 1.1. 二叉树的前序遍历 我们这里要使用栈来进行实现。我们反向思考一下为什么不使用队列如下图前序遍历肯定是先将根结点放进去如果是队列根结点先进先出然后怎么去遍历右子树呢就无法打印的顺序了。 我们定义一个引用cur只要cur不为null就打印值并将该元素放入栈中。当遍历到4时左子树为空返回结点4并弹出再去遍历4的右结点然后返回结点2并弹出让cur等于结点2的右子树并遍历。只要1的左子树没有遍历完1就不弹出。 public class Solution {public void preorderTraversal(TreeNode root){if(root null){return;}StackTreeNode stack new Stack();TreeNode cur root;while(cur ! null){stack.push(cur);System.out.print(cur.val );cur cur.left;}} } 代码写到这里就会出现问题原因是当遍历到结点4的时候4的左子树为空就无法进入while循环。然后把4弹出去让curtop问题又来了如果结点4左边要是不为空又得放入栈中也需要走while循环。 我们会发现当cur走到某个结点时如果为空但栈不为空此时就可以巧妙地在while外面再加一层while循环。 while (cur ! null || !stack.isEmpty()) {while (cur ! null) {stack.push(cur);System.out.print(cur.val );cur cur.left;}cur stack.pop();cur cur.right; } 完整代码实现 import java.util.ArrayList; import java.util.List; import java.util.Stack;class TreeNode{int val;TreeNode left;TreeNode right;public TreeNode() {}public TreeNode(int val) {this.val val;}public TreeNode(int val, TreeNode left, TreeNode right) {this.val val;this.left left;this.right right;} }public class Solution {public ListInteger preorderTraversal(TreeNode root){ListInteger tree new ArrayList();if(root null){return tree;}StackTreeNode stack new Stack();TreeNode cur root;while (cur ! null || !stack.isEmpty()) {while (cur ! null) {tree.add(cur.val);stack.push(cur);cur cur.left;}cur stack.pop();cur cur.right;}return tree;} } import java.util.ArrayList; import java.util.List;public class Test {public static void main(String[] args) {ListInteger result new ArrayList();Solution solution new Solution();TreeNode root new TreeNode(1,new TreeNode(2),new TreeNode(3));root.left.left new TreeNode(4);root.left.right new TreeNode(5);root.left.right.left new TreeNode(6);root.left.right.right new TreeNode(7);root.right.right new TreeNode(8);root.right.right.left new TreeNode(9);result solution.preorderTraversal(root);System.out.println(result);} } 1.2. 二叉树的中序遍历 与前序遍历的思路相同只是打印的时机不一样。中序遍历要在弹出的元素之后直接打印。 完整代码实现 import java.util.ArrayList; import java.util.List; import java.util.Stack;class TreeNode{int val;TreeNode left;TreeNode right;public TreeNode() {}public TreeNode(int val) {this.val val;}public TreeNode(int val, TreeNode left, TreeNode right) {this.val val;this.left left;this.right right;} }public class Solution {public ListInteger inorderTraversal(TreeNode root){ListInteger tree new ArrayList();if(root null){return tree;}StackTreeNode stack new Stack();TreeNode cur root;while (cur ! null || !stack.isEmpty()) {while (cur ! null) {tree.add(cur.val);stack.push(cur);cur cur.left;}cur stack.pop();tree.add(cur.val);cur cur.right;}return tree;} }import java.util.ArrayList; import java.util.List;public class Test {public static void main(String[] args) {ListInteger result new ArrayList();Solution solution new Solution();TreeNode root new TreeNode(1,new TreeNode(2),new TreeNode(3));root.left.left new TreeNode(4);root.left.right new TreeNode(5);root.left.right.left new TreeNode(6);root.left.right.right new TreeNode(7);root.right.right new TreeNode(8);root.right.right.left new TreeNode(9);result solution.inorderTraversal(root);System.out.println(result);} }1.3. 二叉树的后序遍历 后序遍历不能按照我们上面前序与中序的方法来做。如果结点下面还有孩子结点如果把4弹出之后就无法获取它的右侧所以只能获取不能弹出。当右子树为空才能弹出再进行打印。 public class Solution {public void postorderTraversal(TreeNode root){if(root null){return;}StackTreeNode stack new Stack();TreeNode cur root;TreeNode top null;while(cur ! null || !stack.isEmpty()) {while(cur ! null){stack.push(cur);cur cur.left;}top stack.peek();if(top.right null){stack.pop();System.out.print(top.val );}else{cur top.right;}}} }public class Test {public static void main(String[] args) {Solution solution new Solution();TreeNode root new TreeNode(1,new TreeNode(2),new TreeNode(3));root.left.left new TreeNode(4);root.left.right new TreeNode(5);root.left.right.right new TreeNode(7);root.right.right new TreeNode(8);root.right.right.left new TreeNode(9);solution.postorderTraversal(root);} }但这样写会存在问题当遍历到结点5的右结点7时会陷入死循环。那我们怎么知道这个结点被打印过我们再定义引用prev让prev来记录被弹出的结点。 while(cur ! null || !stack.isEmpty()) {while(cur ! null){stack.push(cur);cur cur.left;}top stack.peek();if(top.right null || top.right prev){stack.pop();System.out.print(top.val );prev top;}else{cur top.right;} 完整代码实现 import java.util.ArrayList; import java.util.List; import java.util.Stack;class TreeNode{int val;TreeNode left;TreeNode right;public TreeNode() {}public TreeNode(int val) {this.val val;}public TreeNode(int val, TreeNode left, TreeNode right) {this.val val;this.left left;this.right right;} }public class Solution {public ListInteger postorderTraversal(TreeNode root){ListInteger tree new ArrayList();if(root null){return tree;}StackTreeNode stack new Stack();TreeNode cur root;TreeNode top null;TreeNode prev null;while(cur ! null || !stack.isEmpty()) {while(cur ! null){stack.push(cur);cur cur.left;}top stack.peek();if(top.right null || top.right prev){tree.add(top.val);stack.pop();prev top;}else{cur top.right;}}return tree;} } import java.util.ArrayList; import java.util.List;public class Test {public static void main(String[] args) {ListInteger tree new ArrayList();Solution solution new Solution();TreeNode root new TreeNode(1,new TreeNode(2),new TreeNode(3));root.left.left new TreeNode(4);root.left.right new TreeNode(5);root.left.right.left new TreeNode(6);root.left.right.right new TreeNode(7);root.right.right new TreeNode(8);root.right.right.left new TreeNode(9);tree solution.postorderTraversal(root);System.out.println(tree);} }
http://www.hkea.cn/news/14582373/

相关文章:

  • 选择赣州网站建设发帖软件
  • 铜陵公司做网站python开发工具有哪些
  • 做网站公司需要提供的资料网站建设7大概要多久
  • 如何让订阅号菜单做微网站腾讯官方网站
  • qq邮箱怎么做网站上海注册公司代理电话
  • 镇江市建设工程网站敦化市住房和城乡建设局网站
  • 全球知名购物网站有哪些网站开发公司市场
  • 做网站有回扣拿吗做网站一般哪里找
  • 建网站平台安全性公司网站设计制作公司
  • 域名绑了小程序还可以做网站吗大型网站需要什么样的团队
  • 网页打包app微软优化大师
  • 如果在阿里云上做自己的网站免费网页制作的网站
  • 做网站时需要注意什么qq是哪个公司开发的软件
  • 全网vip视频网站怎么做51源码网
  • 网站建设与运营的收入来源怎么做房地产网站
  • 深圳网站制作招聘怎么重新网站做301
  • 做网站网站犯法吗网页设计制作软件
  • 如何在自己网站上做支付宝吗男女直接做那个视频网站
  • 建立一个网站需要哪些ip网站查询服务器
  • 制作网站演示关于行业网站建设意见
  • jsp企业网站开发前期报告渭南有几个县
  • 很有设计感的企业网站wordpress 多域名绑定
  • 南京网站制作电话个人网站的设计与实现摘要
  • 优速网站建设微信小程序怎么做商城
  • 苏州网站建设熊掌开发公司网签过期
  • 自己电脑做网站服务器设置网站自适应宽度
  • 网站建设公司伟置国外论文类网站有哪些方面
  • 电脑网站转手机版纪念平台网站建设
  • 如何建立国际网站苏州网络公司优惠政策
  • 网站建设公司推荐q479185700顶上如何将自己做的网站放到网上去