File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ // https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
2+ //
3+ // algorithms
4+ // Easy (47.04%)
5+ // Total Accepted: 229,826
6+ // Total Submissions: 488,538
7+ // beats 100.0% of java submissions
8+
9+ /**
10+ * Definition for a binary tree node.
11+ * public class TreeNode {
12+ * int val;
13+ * TreeNode left;
14+ * TreeNode right;
15+ * TreeNode(int x) { val = x; }
16+ * }
17+ */
18+ class Solution {
19+ public List <List <Integer >> levelOrderBottom (TreeNode root ) {
20+ LinkedList <List <Integer >> res = new LinkedList <>();
21+ if (root == null ) {
22+ return res ;
23+ }
24+
25+ LinkedList <TreeNode > q = new LinkedList <>();
26+ ArrayList <Integer > path = new ArrayList <>();
27+ q .add (root );
28+ q .add (null );
29+
30+ while (true ) {
31+ TreeNode curItem = q .pollFirst ();
32+ if (curItem == null ) {
33+ res .offerFirst (new ArrayList <Integer >(path ));
34+ if (q .size () == 0 ) {
35+ break ;
36+ }
37+ path .clear ();
38+ q .add (null );
39+ } else {
40+ path .add (curItem .val );
41+ if (curItem .left != null ) {
42+ q .add (curItem .left );
43+ }
44+ if (curItem .right != null ) {
45+ q .add (curItem .right );
46+ }
47+ }
48+ }
49+
50+ return res ;
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments