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

做数据权威的网站有哪些全国seo公司排名

做数据权威的网站有哪些,全国seo公司排名,手机下载视频网站模板下载,我的世界皮肤网站做二叉树遍历方法总结 二叉树的遍历总体上分为深度优先遍历和广度优先遍历。常见的前中后序三种遍历方式就属于深度优先遍历,遍历过程中是顺着一条路径一直遍历到空节点然后向上回溯继续顺着遍历上一个节点的其他方向。层序遍历属于广度优先遍历,先遍历完同…

二叉树遍历方法总结

 二叉树的遍历总体上分为深度优先遍历和广度优先遍历。常见的前中后序三种遍历方式就属于深度优先遍历,遍历过程中是顺着一条路径一直遍历到空节点然后向上回溯继续顺着遍历上一个节点的其他方向。层序遍历属于广度优先遍历,先遍历完同一层的节点,再接着遍历下一层节点。
 本文主要介绍二叉树三种深度优先遍历方式的实现方式:递归方式和非递归方式。
 递归方式实现如下。
 前序遍历-力扣144:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new LinkedList<>();if (root == null) {return result;}preOrder(root,result);return result;}private void preOrder(TreeNode current,List<Integer> result) {if (current != null) {result.add(current.val);preOrder(current.left,result);preOrder(current.right,result);}}
}

 中序遍历-力扣94,就是把前序中的处理节点的顺序调整一下。

private void inOrder(TreeNode current,List<Integer> result) {if (current != null) {          inOrder(current.left,result);result.add(current.val);inOrder(current.right,result);}
}

 后序遍历-力扣145,将节点处理顺序调整到最后:

private void inOrder(TreeNode current,List<Integer> result) {if (current != null) {          inOrder(current.left,result);           inOrder(current.right,result);result.add(current.val);}
}

 非递归方式实现如下。
 前序遍历-力扣144,利用栈保存访问过的节点,每访问一个节点,就处理一个。

class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new LinkedList<>();if (root == null) {return result;}Deque<TreeNode> stack = new LinkedList<>();stack.push(root);while (!stack.isEmpty()) {TreeNode tempNode = stack.pop();result.add(tempNode.val);if (tempNode.right != null) {stack.push(tempNode.right);}if (tempNode.left != null) {stack.push(tempNode.left);}}return result;}
}

 中序遍历-力扣94

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> result = new LinkedList<>();if (root == null) {return result;}Deque<TreeNode> stack = new LinkedList<>();TreeNode current = root;while (!stack.isEmpty() || current != null) {if (current != null) {stack.push(current);current = current.left;} else {TreeNode tempNode = stack.pop();result.add(tempNode.val);current = tempNode.right;}}return result;} 
}

 后序遍历-力扣145,后序遍历可以当成是把前序遍历顺序改变一下,从前序的中左右变成中右左,然后再把结果倒置。

class Solution {//后序遍历public List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new LinkedList<>();if (root == null) {return result;}Deque<TreeNode> stack = new LinkedList<>();stack.push(root);TreeNode cur = root;while (!stack.isEmpty()) {TreeNode tempNode = stack.pop();result.add(tempNode.val);if (tempNode.left != null) {stack.push(tempNode.left);}if (tempNode.right != null) {stack.push(tempNode.right);}}Collections.reverse(result);return result;}}
http://www.zhongyajixie.com/news/19019.html

相关文章:

  • 网站开发 创造收益比较好的友链平台
  • 中介专门做别墅的网站域名购买平台
  • discuz网站怎么做排名网页制作的软件有哪些
  • 网站建设应考虑哪些方面的问题下载安装
  • 程序员代做网站违法电商培训大概多少学费
  • 北京做网站的公司排行知名seo公司
  • 企业网站排名优化哪家好在线建站网页制作网站建设平台
  • 重庆市建设岗位培训网站杭州seo公司服务
  • 案例展示网站营销软件
  • 个人做网络推广哪个网站好seo蜘蛛池
  • 济南怎样做网站推广中国楼市最新消息
  • 自己做网站代码做网站需要哪些技术
  • 隆尧网站制作识图
  • 山东卓创网络网站建设软文写作什么意思
  • 深圳电子商城网站建设搜索广告是什么
  • 百度收录哪个网站多武汉大学人民医院洪山院区
  • 学做网站论坛vip账号全国疫情最新数据
  • wordpress重定向代码湖北seo
  • 做网站建设费用如何用html制作网页
  • 杭州网站制百度手机助手安卓版下载
  • 深圳疫情通报最新消息深圳防疫措施优化
  • 网站运营适合什么样的人做网站底部友情链接代码
  • icp备案是什么怎么卸载windows优化大师
  • 做企业网站可以没有后台吗网站搭建需要什么
  • 做网站1000以下哪家好写文的免费软件
  • 各种网站推广是怎么做的国内新闻最新5条
  • app网站制作美国传销网站关键词
  • wordpress statraq重庆的seo服务公司
  • 个人做淘宝客网站要备案官网设计比较好看的网站
  • pc端网站转手机站怎么做小说搜索风云榜