File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ /* *
2+ * Example:
3+ * var ti = TreeNode(5)
4+ * var v = ti.`val`
5+ * Definition for a binary tree node.
6+ * class TreeNode(var `val`: Int) {
7+ * var left: TreeNode? = null
8+ * var right: TreeNode? = null
9+ * }
10+ */
11+ class Solution {
12+ fun zigzagLevelOrder (root : TreeNode ? ): List <List <Int >> {
13+
14+ val res = ArrayList <ArrayList <Int >>()
15+ root? : return res
16+
17+ val q = ArrayDeque <TreeNode ?>()
18+ var left = true
19+
20+ q.add(root)
21+
22+ while (q.isNotEmpty()) {
23+ val s = q.size
24+ val temp = ArrayList <Int >()
25+ repeat (s) {
26+ val x = q.poll()!!
27+ if (x.left != null ) q.add(x.left)
28+ if (x.right != null ) q.add(x.right)
29+ if (left) temp.add(x.value)
30+ else temp.add(0 , x.value)
31+ }
32+ res.add(temp)
33+ left = ! left
34+ }
35+
36+ return res
37+ }
38+
39+ val TreeNode .value
40+ get() = this .`val `
41+ }
You can’t perform that action at this time.
0 commit comments