当前位置:网站首页>二叉树的层序遍历 II[层序遍历方式之一 ->递归遍历 + level]
二叉树的层序遍历 II[层序遍历方式之一 ->递归遍历 + level]
2022-06-29 03:16:00 【REN_林森】
分层遍历 ->递归遍历 + level
前言
层次遍历是操作树的基础,而分层遍历就是层次遍历的变体,一般可用队列+层size来分层,也可以递归遍历+level来分层。通过锯齿形分层遍历二叉树来练习前序遍历+level来分层。
一、二叉树的层序遍历 II

二、递归遍历 + level分层
/** * 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<List<Integer>> levelOrderBottom(TreeNode root) {
// 前序遍历 + level 实现层次遍历。
List<List<Integer>> ans = new ArrayList<>();
preOrder(root, ans, 0);
return ans;
}
private void preOrder(TreeNode root, List<List<Integer>> ans, int level) {
// null节点,递归终止。
if (root == null) return;
// 一层一层往下走的,所以选择取等。
if (level == ans.size()) ans.add(0,new ArrayList<>());
List<Integer> cur = ans.get(ans.size() - 1 - level);
cur.add(root.val);
// 前序遍历
preOrder(root.left, ans, level + 1);
preOrder(root.right, ans, level + 1);
}
}
总结
1)层次遍历,分层遍历。
2)队列+层size分层 | 递归遍历 + level分层。
参考文献
边栏推荐
- PWN attack and defense world level2
- Kubernetes: container resource requirements and constraints (constraints)
- Etcd tutorial - Chapter 6 etcd core API V3
- [linear algebra] 1.1 second and third order determinants
- 广发证券开户是真的安全可靠吗
- 数据离散化
- FPGA (VIII) RTL code IV (basic circuit design 1)
- How to add the live video function to the website built by your own live video software (build a live video website)
- Jerry's monitoring alarm clock [chapter]
- FortiGate firewall configuration log uploading regularly
猜你喜欢

Web GIS 航拍实现的智慧园区数字孪生应用

The method of displaying or closing the network flying line in Allegro design

Altium Designer中从已有的PCB中导出所有元件的封装的方法

2022-2028 global long wave infrared camera and camera core industry research and trend analysis report

2022-2028 global low carbon concrete industry research and trend analysis report
![[flutter topic] 66 diagram basic constraints box (I) yyds dry goods inventory](/img/8b/55a43383e1cb6b37231dba980c7b11.jpg)
[flutter topic] 66 diagram basic constraints box (I) yyds dry goods inventory

【线程通信】

Web APIs 高阶函数 丨黑马程序员

In depth analysis of Apache bookkeeper series: Part 3 - reading principle

MATALB signal processing - signal transformation (7)
随机推荐
[Shangshui Shuo series] the simplest subtitle configuration
Tortoise does not display a green Icon
Gartner“客户之声”最高分,用户体验成中国数据库一大突破口
[線性代數] 1.1 二階與三階行列式
归并排序
1110: nearest common ancestor (function topic)
Shell script to count files, then remove oldest files
PAT甲级 A1057 Stack
想当设备管理师?满足这三个报考条件就可以
How to keep source code secret in embedded development
层次分析法(AHP)
Analytic hierarchy process (AHP)
Synchronous movement state of Jerry's watch [chapter]
逆序对对数计算,顺序对对数计算——归并排序
PWN攻防世界Level2
問題——adb shellerror: insufficient permissions for device: verify udev rules.
How to optimize databases and tables
Is the account opening of GF Securities really safe and reliable
PMP Business Analysis Overview
[thread communication]