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

求个没封的w站2021软件营销型网站建设首选

求个没封的w站2021软件,营销型网站建设首选,网站优化提升排名,网站底部怎么修改专栏:Java数据结构秘籍 个人主页:手握风云 目录 一、非递归实现遍历二叉树 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;}Stack<TreeNode> stack = new Stack<>();TreeNode cur = root;while(cur != null){stack.push(cur);System.out.print(cur.val+" ");cur = cur.left;}}
}

        代码写到这里就会出现问题,原因是:当遍历到结点4的时候,4的左子树为空,就无法进入while循环。然后把4弹出去,让cur=top,问题又来了,如果结点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 List<Integer> preorderTraversal(TreeNode root){List<Integer> tree = new ArrayList<>();if(root == null){return tree;}Stack<TreeNode> 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) {List<Integer> 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 List<Integer> inorderTraversal(TreeNode root){List<Integer> tree = new ArrayList<>();if(root == null){return tree;}Stack<TreeNode> 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) {List<Integer> 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;}Stack<TreeNode> 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 List<Integer> postorderTraversal(TreeNode root){List<Integer> tree = new ArrayList<>();if(root == null){return tree;}Stack<TreeNode> 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) {List<Integer> 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.yayakq.cn/news/495893/

相关文章:

  • 哪个网站可以做创意短视频wordpress 开店
  • 连云港企业做网站做淘宝客导购网站推广
  • 有做学历在网站能查的到的精神文明建设网站
  • wordpress全站静太化网页美工设计一套多少钱
  • 城市介绍网站模板企业门户网站开发公司
  • 如果在各大网站做免费的网络推广wordpress多主题插件下载地址
  • 革吉网站建设wordpress加入购买功能
  • 延庆手机网站建设济南网站优化
  • 站长工具seo优化系统做301跳转会影响之前网站排名吗
  • 中山河北建设信息网站做创意美食的视频网站
  • 扁平式的网站网页界面布局
  • 海阳市住房和城乡建设局官方网站网店推广方式有哪些
  • 厦门网站制作计划网站建设应重视后期的服务和维护
  • 企业网站整理优化软件定制与开发
  • 商城网站设计服务商苏州纳米加工平台
  • 单页面网站现在杭州网站建设网络
  • 闵行网站建设哪家好深圳网站建设公司报价
  • mvc网站建设的实验报告做wap网站能火吗
  • 怎么自己在微信上做网站做网站需提供什么资料
  • 做网站的软件帝国个人注册公司需要哪些资料
  • 陕西省建设银行网站6中国建筑集团有限公司天眼查
  • 官方网站英语建设个人网站的策划书
  • 织梦app网站模板qq官网在线登录网页版
  • 建立互联网公司网站网站建设总计
  • 珠海横琴天聚建设工程有限公司网站长安做英文网站
  • 手机改ip地址软件免费潍坊百度推广优化
  • 杭州网站建设报价静安免费网站制作
  • 浙江网站建设公司推荐网站手机端 怎么做
  • 网站制作公司的网站网络维护和故障维修
  • 韩国大型门户网站天堂tv在线观看