|
| 1 | +package tree; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import static org.hamcrest.core.Is.is; |
| 10 | +import static org.junit.Assert.assertThat; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author alis |
| 14 | + * @date 2019/10/27 1:30 AM |
| 15 | + * @description |
| 16 | + */ |
| 17 | +public class LC_590_NTreePostOrderTraversalTest { |
| 18 | + private LC_590_NTreePostOrderTraversal nTreePostOrderTraversal = new LC_590_NTreePostOrderTraversal(); |
| 19 | + |
| 20 | + @Test |
| 21 | + public void postOrderTest() { |
| 22 | + NTreeNode node2 = new NTreeNode(2, Collections.emptyList()); |
| 23 | + NTreeNode node4 = new NTreeNode(4, Collections.emptyList()); |
| 24 | + NTreeNode node3 = new NTreeNode(3, Arrays.asList(new NTreeNode(5, Collections.emptyList()), new NTreeNode(6, Collections.emptyList()))); |
| 25 | + NTreeNode root = new NTreeNode(1, Arrays.asList(node3, node2, node4)); |
| 26 | + List<Integer> res = nTreePostOrderTraversal.postOrderRecur(root); |
| 27 | + List<Integer> excepted = Arrays.asList(5, 6, 3, 2, 4, 1); |
| 28 | + assertThat(res, is(excepted)); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void postOrderStackTest() { |
| 33 | + NTreeNode node2 = new NTreeNode(2, Collections.emptyList()); |
| 34 | + NTreeNode node4 = new NTreeNode(4, Collections.emptyList()); |
| 35 | + NTreeNode node3 = new NTreeNode(3, Arrays.asList(new NTreeNode(5, Collections.emptyList()), new NTreeNode(6, Collections.emptyList()))); |
| 36 | + NTreeNode root = new NTreeNode(1, Arrays.asList(node3, node2, node4)); |
| 37 | + List<Integer> res = nTreePostOrderTraversal.postOrderStack(root); |
| 38 | + List<Integer> excepted = Arrays.asList(5, 6, 3, 2, 4, 1); |
| 39 | + assertThat(res, is(excepted)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void postOrderStackErrorCaseTest() { |
| 44 | + NTreeNode node3 = new NTreeNode(3, Arrays.asList(new NTreeNode(6, Collections.emptyList()))); |
| 45 | + NTreeNode node10 = new NTreeNode(10, Arrays.asList(new NTreeNode(5, Collections.emptyList()), new NTreeNode(0, Collections.emptyList()))); |
| 46 | + NTreeNode root = new NTreeNode(1, Arrays.asList(node10, node3)); |
| 47 | + List<Integer> res = nTreePostOrderTraversal.postOrderStack(root); |
| 48 | + List<Integer> excepted = Arrays.asList(5, 0, 10, 6, 3, 1); |
| 49 | + assertThat(res, is(excepted)); |
| 50 | + } |
| 51 | +} |
0 commit comments