src/main/java/com/leetcode/tree/hard/EncodeNaryTreeToBinaryTree_431.java
public TreeNode encode(Node root) {
      if (root == null) {
        return null;
      }

      Deque<Node> queue = new ArrayDeque<>();
      Deque<TreeNode> results = new ArrayDeque<>();
      TreeNode dummy = new TreeNode(root.val);
      queue.add(root);
      results.add(dummy);

      while (!queue.isEmpty()) {
        Node curr = queue.remove();
        TreeNode currRes = results.remove();
        TreeNode loopDummy = new TreeNode();
        TreeNode currDummy = loopDummy;
        for (Node n : curr.children) {
          TreeNode left = new TreeNode(n.val);
          if (n.children.size() > 0) {
            queue.add(n);
            results.add(left);
          }
          currDummy.left = left;
          currDummy = currDummy.left;
        }
        currRes.right = loopDummy.left;
      }
      return dummy;
    }
// Encodes an n-ary tree to a binary tree.
Encodes an n-ary tree to a binary tree.
Encodes an n - ary tree to a binary tree .
public TreeNode encode(Node root) {
      if (root == null) {
        return null;
      }
      Deque<Node> queue = new ArrayDeque<>();
      Deque<TreeNode> results = new ArrayDeque<>();
      TreeNode dummy = new TreeNode(root.val);
      queue.add(root);
      results.add(dummy);
      while (!queue.isEmpty()) {
        Node curr = queue.remove();
        TreeNode currRes = results.remove();
        TreeNode loopDummy = new TreeNode();
        TreeNode currDummy = loopDummy;
        for (Node n : curr.children) {
          TreeNode left = new TreeNode(n.val);
          if (n.children.size() > 0) {
            queue.add(n);
            results.add(left);
          }
          currDummy.left = left;
          currDummy = currDummy.left;
        }
        currRes.right = loopDummy.left;
      }
      return dummy;
    }
public TreeNode encode ( Node root ) { if ( root == null ) { return null ; } Deque < Node > queue = new ArrayDeque < > ( ) ; Deque < TreeNode > results = new ArrayDeque < > ( ) ; TreeNode dummy = new TreeNode ( root . val ) ; queue . add ( root ) ; results . add ( dummy ) ; while ( ! queue . isEmpty ( ) ) { Node curr = queue . remove ( ) ; TreeNode currRes = results . remove ( ) ; TreeNode loopDummy = new TreeNode ( ) ; TreeNode currDummy = loopDummy ; for ( Node n : curr . children ) { TreeNode left = new TreeNode ( n . val ) ; if ( n . children . size ( ) > 0 ) { queue . add ( n ) ; results . add ( left ) ; } currDummy . left = left ; currDummy = currDummy . left ; } currRes . right = loopDummy . left ; } return dummy ; }
Encodes an n-ary tree to a binary tree.
Encodes an n - ary tree to a binary tree .