package com.fishercoder.solutions;

import com.fishercoder.common.classes.Node;
import java.util.ArrayList;
import java.util.List;

/**
 * 590. N-ary Tree Postorder Traversal
 *
 * Given an n-ary tree, return the postorder traversal of its nodes' values.
 *
 * For example, given a 3-ary tree:
 *
 *              1
 *            / | \
 *           3  2  4
 *          / \
 *         5   6
 *
 * Return its postorder traversal as: [5,6,3,2,4,1].
 *
 * Note:
 *
 * Recursive solution is trivial, could you do it iteratively?
 */
public class _590 {
  public static class Solution1 {
    public List<Integer> postorder(Node root) {
      List<Integer> result = new ArrayList<>();
      if (root == null) {
        return result;
      }
      dfs(root, result);
      result.add(root.val);
      return result;
    }

    private void dfs(Node root, List<Integer> result) {
      if (root == null) {
        return;
      }
      if (root.children.size() > 0) {
        for (Node child : root.children) {
          dfs(child, result);
          result.add(child.val);
        }
      }
    }
  }
}

@@ -73,16 +73,17 @@
 s.Node;%0A
+%0A
 import j
@@ -130,403 +130,8 @@
 t;%0A%0A
-/**%0A * 590. N-ary Tree Postorder Traversal%0A *%0A * Given an n-ary tree, return the postorder traversal of its nodes' values.%0A *%0A * For example, given a 3-ary tree:%0A *%0A *              1%0A *            / %7C %5C%0A *           3  2  4%0A *          / %5C%0A *         5   6%0A *%0A * Return its postorder traversal as: %5B5,6,3,2,4,1%5D.%0A *%0A * Note:%0A *%0A * Recursive solution is trivial, could you do it iteratively?%0A */%0A
 publ
@@ -148,16 +148,18 @@
 590 %7B%0A  
+ 
+ 
 public s
@@ -182,16 +182,20 @@
 tion1 %7B%0A
+    
     publ
@@ -240,16 +240,22 @@
 %7B%0A      
+    
+  
 List%3CInt
@@ -288,16 +288,22 @@
 st%3C%3E();%0A
+      
       if
@@ -312,32 +312,40 @@
 root == null) %7B%0A
+        
         return r
@@ -357,24 +357,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A  
       
+    
 dfs(root
@@ -398,24 +398,30 @@
 ult);%0A      
+     
+ 
 result.add(r
@@ -426,24 +426,30 @@
 (root.val);%0A
+      
       return
@@ -465,11 +465,19 @@
     
+ 
-%7D%0A%0A
+   %7D%0A%0A    
     
@@ -524,24 +524,30 @@
 %3E result) %7B%0A
+      
       if (ro
@@ -564,24 +564,32 @@
 ) %7B%0A        
+   
+     
 return;%0A    
@@ -586,26 +586,38 @@
 turn;%0A      
+    
-%7D%0A
+  %7D%0A      
       if (ro
@@ -638,24 +638,32 @@
 ze() %3E 0) %7B%0A
+        
         for 
@@ -699,24 +699,34 @@
 %7B%0A          
+        
+  
 dfs(child, r
@@ -735,32 +735,42 @@
 ult);%0A          
+      
+    
 result.add(child
@@ -788,24 +788,44 @@
     
+    
-%7D%0A      %7D%0A    %7D%0A
+    %7D%0A            %7D%0A        %7D%0A  
   %7D%0A

refactor 590