Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b449afd

Browse files
committed
zigzag traversal: done
1 parent 8a09892 commit b449afd

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.leetcode.trees;
2+
3+
import java.util.*;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
/**
8+
* Level: Medium
9+
* Link: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
10+
* Description:
11+
* Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then
12+
* right to left for the next level and alternate between).
13+
*
14+
* For example:
15+
* Given binary tree [3,9,20,null,null,15,7],
16+
* 3
17+
* / \
18+
* 9 20
19+
* / \
20+
* 15 7
21+
* return its zigzag level order traversal as:
22+
* [
23+
* [3],
24+
* [20,9],
25+
* [15,7]
26+
* ]
27+
*
28+
* @author rampatra
29+
* @since 2019年08月11日
30+
*/
31+
public class BinaryTreeZigZagLevelOrderTraversal {
32+
33+
/**
34+
* Time Complexity:
35+
* Space Complexity:
36+
* Runtime: <a href="https://leetcode.com/submissions/detail/250830618/">1 ms</a>.
37+
*
38+
* @param root
39+
* @return
40+
*/
41+
public static List<List<Integer>> zigzagLevelOrder(TreeNode root) {
42+
43+
int levelNo = 0;
44+
LinkedList<Integer> currLevel = new LinkedList<>();
45+
List<List<Integer>> levelOrderTraversal = new LinkedList<>();
46+
47+
if (root == null) {
48+
return levelOrderTraversal;
49+
}
50+
51+
Queue<TreeNode> queue = new LinkedList<>();
52+
queue.add(root);
53+
queue.add(null);
54+
55+
while (!queue.isEmpty()) {
56+
57+
TreeNode treeNode = queue.poll();
58+
59+
if (treeNode == null) {
60+
levelOrderTraversal.add(currLevel);
61+
currLevel = new LinkedList<>();
62+
levelNo++;
63+
64+
if (queue.size() > 0) {
65+
queue.add(null);
66+
}
67+
} else {
68+
if (levelNo % 2 == 0) {
69+
currLevel.add(treeNode.val);
70+
} else {
71+
currLevel.add(0, treeNode.val);
72+
}
73+
if (treeNode.left != null) queue.add(treeNode.left);
74+
if (treeNode.right != null) queue.add(treeNode.right);
75+
}
76+
}
77+
78+
return levelOrderTraversal;
79+
}
80+
81+
public static void main(String[] args) {
82+
/*
83+
Binary Tree
84+
85+
1
86+
/ \
87+
2 3
88+
/ \
89+
4 5
90+
*/
91+
TreeNode tree = new TreeNode(1);
92+
tree.left = new TreeNode(2);
93+
tree.right = new TreeNode(3);
94+
tree.left.left = new TreeNode(4);
95+
tree.left.right = new TreeNode(5);
96+
97+
assertEquals("[[1], [3, 2], [4, 5]]", zigzagLevelOrder(tree).toString());
98+
assertEquals("[]", zigzagLevelOrder(null).toString());
99+
}
100+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /