-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0103_Binary_Tree_Zigzag_Level_Order_Traversal.java
48 lines (46 loc) · 1.4 KB
/
0103_Binary_Tree_Zigzag_Level_Order_Traversal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* 103. Binary Tree Zigzag Level Order Traversal
* Problem Link: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
* Difficulty: Medium
*
* Solution Created by: Muhammad Khuzaima Umair
* LeetCode : https://leetcode.com/mkhuzaima/
* Github : https://github.com/mkhuzaima
* LinkedIn : https://www.linkedin.com/in/mkhuzaima/
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
traverse(root, list, 0, false);
return list;
}
private void traverse(TreeNode root, List<List<Integer>> list, int level, boolean toAddAtStart) {
if (root == null) return;
if (list.size() <= level) {
list.add(new ArrayList<>());
}
if (toAddAtStart) {
list.get(level).add(0, root.val);
}
else {
list.get(level).add(root.val);
}
traverse(root.left, list, level+1, !toAddAtStart);
traverse(root.right, list, level+1, !toAddAtStart);
}
}