102. Binary Tree Level Order Traversal
Posted 我的名字叫周周
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了102. Binary Tree Level Order Traversal相关的知识,希望对你有一定的参考价值。
/* * 102. Binary Tree Level Order Traversal * 11.21 by Mingyang 最基本的queue的做法 */ public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> results = new ArrayList<List<Integer>>(); ArrayList<Integer> result = new ArrayList<Integer>(); if (root == null) return results; LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); int currentNumber = 1; int nextNumber = 0; while (queue.size() != 0) { TreeNode temp = queue.poll(); result.add(temp.val); currentNumber--; if (temp.left != null) { queue.add(temp.left); nextNumber++; } if (temp.right != null) { queue.add(temp.right); nextNumber++; } if (currentNumber == 0) { results.add(result); currentNumber = nextNumber; nextNumber = 0; result = new ArrayList<Integer>(); } } return results; }
以上是关于102. Binary Tree Level Order Traversal的主要内容,如果未能解决你的问题,请参考以下文章
102. Binary Tree Level Order Traversal
LeetCode102. Binary Tree Level Order Traversal
102Binary Tree Level Order Traversal
102. Binary Tree Level Order Traversal