Binary Tree Level Order Traversal Solutions in Java
Number 102
Difficulty Medium
Acceptance 54.7%
Link LeetCode
Solutions
Java solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/binary-tree-level-order-traversal/// Inspired by : http://www.jiuzhang.com/solutions/binary-tree-level-order-traversal/// Author : Lei Cao// Date : 2015-10-08package binaryTreeLevelOrderTraversal;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Queue;public class binaryTreeLevelOrderTraversal {/*** @param root: The root of binary tree.* @return: Level order a list of lists of integer*/public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> results = new ArrayList<>();if (root == null) {return results;}ArrayList<Integer> values = new ArrayList<Integer>();Queue<TreeNode> q = new LinkedList<>();q.offer(root);q.offer(null);while (q.size() > 0) {TreeNode node = q.poll();// null node used as a separator of every levelif (node == null) {results.add(new ArrayList<>(values));values.clear();if (q.size() == 0) {break;}q.offer(null);continue;}values.add(node.val);if (node.left != null) {q.offer(node.left);}if (node.right != null) {q.offer(node.right);}}return results;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description//// Author : liuyubobobo/// Time : 2017-11-17import java.util.ArrayList;import java.util.List;import java.util.LinkedList;import javafx.util.Pair;/// BFS/// Time Complexity: O(n), where n is the number of nodes in the tree/// Space Complexity: O(n)class Solution {public List<List<Integer>> levelOrder(TreeNode root) {ArrayList<List<Integer>> res = new ArrayList<>();if(root == null)return res;LinkedList<Pair<TreeNode, Integer>> queue = new LinkedList<>();queue.addLast(new Pair<>(root, 0));while(!queue.isEmpty()){Pair<TreeNode, Integer> front = queue.removeFirst();TreeNode node = front.getKey();int level = front.getValue();if(level == res.size())res.add(new ArrayList<>());assert level < res.size();res.get(level).add(node.val);if(node.left != null)queue.addLast(new Pair<>(node.left, level + 1));if(node.right != null)queue.addLast(new Pair<>(node.right, level + 1));}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description//// Author : liuyubobobo/// Time : 2018-10-16import java.util.ArrayList;import java.util.List;import java.util.LinkedList;import java.util.Queue;/// BFS/// No need to store level information in the queue :-)////// Time Complexity: O(n), where n is the number of nodes in the tree/// Space Complexity: O(n)class Solution2 {public List<List<Integer>> levelOrder(TreeNode root) {ArrayList<List<Integer>> res = new ArrayList<>();if(root == null)return res;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);int levelNum = 1;while(!queue.isEmpty()){int newLevelNum = 0;ArrayList<Integer> level = new ArrayList<>();for(int i = 0; i < levelNum; i ++){TreeNode node = queue.remove();level.add(node.val);if(node.left != null){queue.add(node.left);newLevelNum ++;}if(node.right != null){queue.add(node.right);newLevelNum ++;}}res.add(level);levelNum = newLevelNum;}return res;}}