File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
binary-tree-level-order-traversal Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * public class TreeNode {
4+ * int val;
5+ * TreeNode left;
6+ * TreeNode right;
7+ * TreeNode() {}
8+ * TreeNode(int val) { this.val = val; }
9+ * TreeNode(int val, TreeNode left, TreeNode right) {
10+ * this.val = val;
11+ * this.left = left;
12+ * this.right = right;
13+ * }
14+ * }
15+ */
16+ class Solution {
17+ public List <List <Integer >> levelOrder (TreeNode root ) {
18+
19+
20+ List <List <Integer >> list = new ArrayList <>();
21+ if (root ==null )
22+ {
23+ return list ;
24+ }
25+ Deque <TreeNode > que =new ArrayDeque <>();
26+
27+ que .add (root );
28+ int count =0 ;
29+ while (que .size ()>0 )
30+ {
31+
32+ count =que .size ();
33+ ArrayList <Integer > al =new ArrayList <>();
34+
35+
36+ for (int i =0 ;i <count ;i ++)
37+ {
38+ TreeNode node =que .remove ();
39+ al .add (node .val );
40+ if (node .left !=null )
41+ que .add (node .left );
42+ if (node .right !=null )
43+ que .add (node .right );
44+
45+
46+ }
47+ list .add (al );
48+ }
49+
50+ return list ;
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments