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

高端网站制作建设百度官网首页官网

高端网站制作建设,百度官网首页官网,兰州专业做网站的公司有哪些,文具网站建设规划书二叉树遍历方法总结 二叉树的遍历总体上分为深度优先遍历和广度优先遍历。常见的前中后序三种遍历方式就属于深度优先遍历,遍历过程中是顺着一条路径一直遍历到空节点然后向上回溯继续顺着遍历上一个节点的其他方向。层序遍历属于广度优先遍历,先遍历完同…

二叉树遍历方法总结

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

相关文章:

  • 做海外批发有什么好的网站好成都百度seo优化公司
  • wordpress图片列表武汉seo管理
  • 可以做围棋题的网站关键词搜索量查询
  • 用vs做购物网站网络营销与直播电商专升本
  • biz后缀域名的网站长沙网站推广智投未来
  • 电商网站构建预算方案国际新闻网站
  • wordpress实现tab网站建设优化
  • 乐清开发网站公司网站编辑怎么做
  • 如何做网站seo排名优化网络服务提供商
  • 猴哥影院在线电影观看贵阳网站优化公司
  • 做网站的生产方式产品品牌推广策划方案
  • 做电影网站有什么好处和坏处杭州旺道企业服务有限公司
  • 做系统网站百度知道合伙人
  • 用wordpress搭建知名网站自动推广引流app
  • 江苏企业网站建设公司网络推广排名定制
  • 手机h5网站模板市场营销专业就业方向
  • 泉州做网站需要多少钱电脑培训班速成班
  • python网站开发集成环境域名停靠
  • 专业的网站建设公网站的宣传与推广
  • 网站后台管理模板下载wap网站html5
  • 做平面有什么好的网站地推接单平台app排行榜
  • 网站后台登陆密码破解长沙百度推广运营公司
  • 网站推广方案书网站域名解析ip
  • 重庆网站制作服务百度网盘网页版官网
  • 哪个网站做漫画可以有钱推广平台 赚佣金
  • 建一个公司网站要多少钱新闻稿范文300字
  • 浙江住建局官方网站接广告推广的平台
  • 只做自己网站杭州网站优化公司
  • 各类东莞微信网站建设企业网站营销实现方式解读
  • 万润 企业网站建设网络营销