当前位置:网站首页>二叉树相关知识
二叉树相关知识
2022-07-25 23:58:00 【Coding~Man】
1:二叉树的层序遍历->通过队列while实现
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root == null){
return res;
}
Queue<TreeNode> que = new LinkedList<TreeNode>();
que.offer(root);
while(!que.isEmpty()){
List<Integer> level = new ArrayList<Integer>();
int cursize = que.size();
for(int i= 1;i<= cursize ;i++){
TreeNode node = que.poll();
level.add(node.val);
if(node.left != null){
que.offer(node.left);
}
if(node.right != null){
que.offer(node.right);
}
}
res.add(level);
}
return res;
}}
二叉树的层序遍历->递归实现
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();//存储最后结果
level(root,0,res);
return res;
}
public void level(TreeNode root,int level,List<List<Integer>> res){
if(root == null){
return ;
}
if(res.size() == level){
List<Integer> list = new ArrayList<Integer>();
list.add(root.val);
res.add(list);
}else{
res.get(level).add(root.val);
}
level(root.left, level+1, res);
level(root.right,level+1,res);
}
}
边栏推荐
- typescript ts 基础知识之类
- Programming password guessing game
- 多御安全浏览器手机版将增加新功能,使用户浏览更个性化
- Are you still using your browser's own bookmarks? This bookmark plugin is awesome
- 二叉树——111. 二叉树的最小深度
- Nacos 下线服务时报错 errCode: 500
- 【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
- Promise resolve callback hell, async await modifier
- Pytorch学习记录(一):Pytorch 简介
- 2022-07-18 study notes of group 5 self-cultivation class (every day)
猜你喜欢

Write a select drop-down list

What is parity? How to use C language?

Kubernetes网络插件详解 - Calico篇 - 概述

Stm32 systeminit trap during simulation debugging

Call nailing API and report an error: the signature sent by the robot is expired; Solution: please keep the signature generation time and sending time within timestampms

结对编程实践心得

下一代终端安全管理的关键特征与应用趋势

Leetcode200-查找岛屿数量详解

Bubble sort idea and Implementation

回溯——77. 组合
随机推荐
C# - readonly 和 const 关键字
[ManageEngine] servicedesk plus won the 2022 safety model engineering data safety award
Interview focus - TCP protocol of transport layer
1223. Dice simulation range DP
Exercise (1) create a set C1 to store the elements "one", "two", "three"“
The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
Key and difficult points of C language pointer
Cherish time and improve efficiency
Leetcode107-二叉树的层序遍历II详解
抽丝剥茧C语言(高阶)程序环境和预处理
Getting started with Servlet
调用钉钉api报错:机器人发送签名过期;solution:签名生成时间和发送时间请保持在 timestampms 以内
How does JS judge whether the current date is within a certain range
NVIDIA cuDNN学习
[learning notes] unreal 4 engine introduction (IV)
Nacos 下线服务时报错 errCode: 500
@The underlying principle of Autowired annotation
The late Apple co-founder Steve Jobs was posthumously awarded the U.S. presidential medal of freedom
Storage of data in memory
The process of finding free screen recording software - I didn't expect win10 to come with this function