二叉树的层序遍历算法 + 打印二叉树所有最左边的元素(算法)

层序遍历

/**
* 树结构定义
*/
private static class BinaryNode<T> {BinaryNode(T theElement) {this(theElement, null, null);}BinaryNode(T theElement, BinaryNode<T> lt, BinaryNode<T> rt) {element = theElement;left = lt;right = rt;}T element;BinaryNode<T> left;BinaryNode<T> right;
}//层序遍历方法
public static void levelRead(BinaryNode node) {if (node == null) {return;}//计算深度int depth = calcDepth(node);for (int i = 0; i < depth; i++) {//打印每个深度上的所有节点readLevel(node,i);}}private static void readLevel(BinaryNode node, int i) {if (node == null||i<1) {return;}//递归打印,如果层级为1,则打印当前节点,如果不为1,则说明还在比较高的等级,需要递归从头往下继续找当前层。if (i == 1) {System.out.print(node.element+"  ");}readLevel(node.left, i-1);readLevel(node.right, i-1);}private static int calcDepth(BinaryNode node) {if (node ==null) {return 0;}int ld = calcDepth(node.left);int rd = calcDepth(node.right);if (ld>rd) {return ld+1;}else{return rd+1;}
}

打印二叉树所有最左边的元素

在层序遍历的基础上,我们可以借此实现打印二叉树上所有最左边的元素,代码如下:

public static void levelReadLeft(BinaryNode node) {if (node == null) {return;}int depth = calcDepth(node);for (int i = 1; i <= depth; i++) {String string = readLevelLeft(node,i);System.out.println(string);}
}private static String readLevelLeft(BinaryNode node, int i) {String reString = "";if (node == null||i<1) {return reString;}if (i == 1) {return reString + (node.element+"  ");}reString += readLevelLeft(node.left, i-1);if (reString.equals ("")) {reString += readLevelLeft(node.right, i-1);}return reString;
}private static int calcDepth(BinaryNode node) {if (node ==null) {return 0;}int ld = calcDepth(node.left);int rd = calcDepth(node.right);if (ld>rd) {return ld+1;}else{return rd+1;}
}

从顶层遍历最右边节点

代码如下:

 public static void levelReadRight(BinaryNode node) {if (node == null) {return;}int depth = calcDepth(node);for (int i = 1; i <= depth; i++) {String string = readLevelRight(node,i);System.out.println(string);}
}private static String readLevelRight(BinaryNode node, int i) {String reString = "";if (node == null||i<1) {return reString;}if (i == 1) {return reString + (node.element+"  ");}reString += readLevelRight(node.right, i-1);if (reString.equals ("")) {reString += readLevelRight(node.left, i-1);}return reString;
}private static int calcDepth(BinaryNode node) {if (node ==null) {return 0;}int ld = calcDepth(node.left);int rd = calcDepth(node.right);if (ld>rd) {return ld+1;}else{return rd+1;}
}

二叉树的层序遍历算法 + 打印二叉树所有最左边的元素(算法)相关推荐

  1. 代码随想录【day 14 二叉树】| 层序遍历 226.翻转二叉树 101.对称二叉树

    代码随想录[day 14 二叉树]| 层序遍历 226.翻转二叉树 101.对称二叉树 层序遍历 卡哥文解 视频讲解 题目链接:102.二叉树的层序遍历 解题思路 代码实现 题目链接:107.二叉树的 ...

  2. 代码随想录算法训练营第十五天 | 102. 二叉树的层序遍历 | 226.翻转二叉树 | 101. 对称二叉树

    递归三部曲 确定递归函数的参数和返回值 确定终止条件 确定单层递归的逻辑 102. 二叉树的层序遍历 题解及想法 解法一 :递归法 class Solution {public List<Lis ...

  3. 二叉树的层序遍历_NC15 求二叉树的层序遍历

    NC15 求二叉树的层序遍历 考过的企业 - 小米 题目描述 给定一个二叉树,返回该二叉树层序遍历的结果,(从左到右,一层一层地遍历) 例如:给定的二叉树是{3,9,20,#,#,15,7},该二叉树 ...

  4. c++二叉树的层序遍历_leetcode 103. 二叉树的锯齿形层序遍历

    按层次遍历,记录下对应节点的val和所在层,然后经过一定变换得到输出.python代码如下: # Definition for a binary tree node.# class TreeNode( ...

  5. 代码随想录算法训练营第十三天|102.二叉树的层序遍历、226.翻转二叉树、101.对称二叉树

    链接:代码随想录 文章目录 102.二叉树的层序遍历 226.翻转二叉树 101.对称二叉树 解题方法 题目思路 示例代码 总结 解题思路 1.102.二叉树的层次遍历 解题思路 递归法,创立二维数组 ...

  6. 【LeetCode102. 二叉树的层序遍历】——层序遍历

    102. 二叉树的层序遍历 给你二叉树的根节点 root ,返回其节点值的 层序遍历 . (即逐层地,从左到右访问所有节点). 示例 1: 输入:root = [3,9,20,null,null,15 ...

  7. 树:二叉树的层序遍历算法(超简洁实现及详细分析)

    实现思路 我们来看看下图的二叉链表 如何实现层序遍历. 层序遍历顺序:ABECDG A为B.E的双亲结点,遍历顺序是 根->左->右 是不是. 而且每个结点都是这样的遍历顺序 有木有.那么 ...

  8. 算法--- 二叉树的层序遍历 II

    题目 二叉树的层序遍历 II 给定一个二叉树,返回其节点值自底向上的层序遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)例如: 给定二叉树 [3,9,20,null,null,15, ...

  9. 代码随想录算法训练营第15天,102.二叉树的层序遍历、226.翻转二叉树、101.对称二叉树

    102.二叉树的层序遍历.226.反转二叉树.101.对称二叉树 102.二叉树的层序遍历 二叉树的层次遍历,我们可以定义一个队列, 当访问到某一个节点时,我们将它存在的左右节点放入队列中,便可达到按 ...

最新文章

  1. 一文了解Spring Cloud Stream体系
  2. 【S操作】老铁留步,干货来了!小总结云存储云办公云笔记工具——我的云工具选择,供您参考...
  3. 2020年财富金字塔出炉,你距离高净值还有多远?
  4. [Qt教程] 第48篇 进阶(八) 3D绘图简介
  5. android service中显示一个dialog
  6. Angular 4.x 事件管理器及自定义EventManagerPlugin
  7. 安卓简单天气预报app源码_七个个小众但实用的APP,效率翻倍~
  8. 【Flink】Flink TaskManager 一直 User file cache uses directory
  9. Arithmetic Sequence
  10. 怎么批量查找关键词-批量查找关键词软件工具
  11. 数学建模MATLAB之分析法(一)
  12. Blender建模(一)
  13. 订阅者Subscriber的编程实现
  14. 【好书推荐】物理、数学和发明的经典科普书籍
  15. Kafka的概念与命令操作
  16. sqrt函数实现(涉及3D游戏引擎源码)
  17. Latex 表格文字居中(垂直和水平居中)
  18. 设计模式(四)行为型模式介绍及实例(上)
  19. 如何用Excel做一个战斗模拟器(三)战斗过程模拟
  20. 将ocx和DLL文件打包成cab文件,实现IE浏览器在线安装

热门文章

  1. 如何保存文件为c语言格式,急求如何将下列C语言程序数据存储到文件中?
  2. python编码转换语句_好程序员Python教程之字符串编码知识小结
  3. android 调用本地第三方应用软件,如qq、微信、微博和视频播放器等
  4. 八十五、store数据,actionCreators 与 constants 的拆分和redux-immutable的使用
  5. 六十七、完成Vue项目首页图标区域布局和逻辑实现
  6. 五十六、 白话讲解商业智能 BI、数据仓库 DW、数据挖掘 DM
  7. node mysql limit_node中mysql连接池的connectionLimit指什么,它和mysql的最小连接数和最大连接数的关系是什么?...
  8. NeurIPS 2020 | FixMatch:通过图像增强就能实现半监督学习
  9. 首个金融领域的开源中文预训练语言模型FinBERT了解下
  10. python-time.time()