博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
102. Binary Tree Level Order Traversal
阅读量:5922 次
发布时间:2019-06-19

本文共 4983 字,大约阅读时间需要 16 分钟。

题目:

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:

Given binary tree {3,9,20,#,#,15,7},

3   / \  9  20    /  \   15   7

 

return its level order traversal as:

[  [3],  [9,20],  [15,7]]

 

confused what "{1,#,2,3}" means? 

链接: 

题解:

层序遍历二叉树。使用BFS, 用一个Queue来辅助存储当前层的节点。

Time Complexity - O(n), Space Complexity - O(n) (最多一层节点数)

public class Solution {    public ArrayList
> levelOrder(TreeNode root) { ArrayList
> result = new ArrayList
>(); if(root == null) return result; Queue
queue = new LinkedList
(); ArrayList
list = new ArrayList
(); queue.add(root); int curLevel = 1, nextLevel = 0; while(!queue.isEmpty()){ TreeNode node = queue.poll(); curLevel --; list.add(node.val); if(node.left != null){ queue.offer(node.left); nextLevel ++; } if(node.right != null){ queue.offer(node.right); nextLevel ++; } if(curLevel == 0){ curLevel = nextLevel; nextLevel = 0; result.add(new ArrayList
(list)); list.clear(); } } return result; }}

 

Update:

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public List
> levelOrder(TreeNode root) { List
> res = new ArrayList<>(); if(root == null) return res; Queue
queue = new LinkedList<>(); //BFS ArrayList
list = new ArrayList<>(); queue.add(root); //Initialize int curLevel = 1, nextLevel = 0; while(!queue.isEmpty()) { TreeNode node = queue.poll(); list.add(node.val); curLevel --; if(node.left != null) { queue.offer(node.left); nextLevel++; } if(node.right != null) { queue.offer(node.right); nextLevel++; } if(curLevel == 0) { curLevel = nextLevel; nextLevel = 0; res.add(new ArrayList
(list)); list.clear(); } } return res; }}

 

 二刷:

使用一个queue来帮助BFS层序遍历,有两个记录当前层节点和下一层节点数的变量curLevel以及nextLevel。

Java:

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public List
> levelOrder(TreeNode root) { List
> res = new ArrayList<>(); if (root == null) { return res; } Queue
q = new LinkedList<>(); q.offer(root); int curLevel = 1, nextLevel = 0; List
list = new ArrayList<>(); while (!q.isEmpty()) { TreeNode node = q.poll(); curLevel--; list.add(node.val); if (node.left != null) { q.offer(node.left); nextLevel++; } if (node.right != null) { q.offer(node.right); nextLevel++; } if (curLevel == 0) { curLevel = nextLevel; nextLevel = 0; res.add(new ArrayList
(list)); list.clear(); } } return res; }}

 

三刷:

不要忘记把结果保存到结果集里,并且也要clear当前层。

Java:

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public List
> levelOrder(TreeNode root) { List
> res = new ArrayList<>(); if (root == null) { return res; } List
level = new ArrayList<>(); Queue
q = new LinkedList<>(); q.offer(root); int curLevel = 1, nextLevel = 0; while (!q.isEmpty()) { TreeNode node = q.poll(); curLevel--; level.add(node.val); if (node.left != null) { q.offer(node.left); nextLevel++; } if (node.right != null) { q.offer(node.right); nextLevel++; } if (curLevel == 0) { curLevel = nextLevel; nextLevel = 0; res.add(new ArrayList
(level)); level.clear(); } } return res; }}

 

Update: 换种写法

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public List
> levelOrder(TreeNode root) { List
> res = new ArrayList<>(); if (root == null) return res; Queue
q = new LinkedList<>(); q.offer(root); int curLevel = 1; List
list = new ArrayList<>(); while (!q.isEmpty()) { TreeNode node = q.poll(); curLevel--; list.add(node.val); if (node.left != null) q.offer(node.left); if (node.right != null) q.offer(node.right); if (curLevel == 0) { curLevel = q.size(); res.add(new ArrayList<>(list)); list.clear(); } } return res; }}

 

 

Reference:

https://leetcode.com/discuss/21778/java-solution-using-dfs

https://leetcode.com/discuss/22533/java-solution-with-a-queue-used

https://leetcode.com/discuss/58454/java-queue-solution-beats-100%25

转载地址:http://tmivx.baihongyu.com/

你可能感兴趣的文章
setLocale的一个用处
查看>>
delphi 预览图片2 (MouseUP)
查看>>
Laravel5中防止XSS跨站攻击的方法
查看>>
陈天桥:欣赏360保护隐私 用户安全永远第一
查看>>
内网终端管理:独立或统一管理都将大行其道
查看>>
汉字和Unicode编码知识
查看>>
利用奇异值分解(SVD)简化数据
查看>>
SQL Server 2008 未来将不再包含全文检索功能, 再研究此功能已经没多大意思了.
查看>>
他们写的,一点思考,一点敬意
查看>>
python初级学习笔记
查看>>
JMeter使用技巧
查看>>
linux -- Ubuntu14.04及之后版本重启网卡不生效
查看>>
sharepoint2010 数据访问的方式
查看>>
SharePoint 2013 工作流之Visual Studio开发示例篇
查看>>
C# 中New关键字的用法
查看>>
Activity静态变量传递参数
查看>>
MongoDB的复制集
查看>>
js操作符
查看>>
css中常用的标签
查看>>
C# Dapper 简单实例
查看>>