src/main/java/com/leetcode/tree/hard/EncodeNaryTreeToBinaryTree_431.java
public Node decode(TreeNode root) {
      if (root == null) {
        return null;
      }
      Node result = new Node(root.val, new ArrayList<>());
      if (root.right == null) {
        return result;
      }

      Deque<TreeNode> deque = new ArrayDeque<>();
      Deque<Node> resDeque = new ArrayDeque<>();
      resDeque.add(result);
      deque.add(root.right);

      while (!deque.isEmpty()) {
        TreeNode curr = deque.remove();
        Node currRes = resDeque.remove();
        while (curr != null) {
          Node n = new Node(curr.val, new ArrayList<>());
          currRes.children.add(n);
          if (curr.right != null) {
            resDeque.add(n);
            deque.add(curr.right);
          }
          curr = curr.left;
        }
      }
      return result;
    }
// Decodes your binary tree to an n-ary tree.
Decodes your binary tree to an n-ary tree.
Decodes your binary tree to an n - ary tree .
public Node decode(TreeNode root) {
      if (root == null) {
        return null;
      }
      Node result = new Node(root.val, new ArrayList<>());
      if (root.right == null) {
        return result;
      }
      Deque<TreeNode> deque = new ArrayDeque<>();
      Deque<Node> resDeque = new ArrayDeque<>();
      resDeque.add(result);
      deque.add(root.right);
      while (!deque.isEmpty()) {
        TreeNode curr = deque.remove();
        Node currRes = resDeque.remove();
        while (curr != null) {
          Node n = new Node(curr.val, new ArrayList<>());
          currRes.children.add(n);
          if (curr.right != null) {
            resDeque.add(n);
            deque.add(curr.right);
          }
          curr = curr.left;
        }
      }
      return result;
    }
public Node decode ( TreeNode root ) { if ( root == null ) { return null ; } Node result = new Node ( root . val , new ArrayList < > ( ) ) ; if ( root . right == null ) { return result ; } Deque < TreeNode > deque = new ArrayDeque < > ( ) ; Deque < Node > resDeque = new ArrayDeque < > ( ) ; resDeque . add ( result ) ; deque . add ( root . right ) ; while ( ! deque . isEmpty ( ) ) { TreeNode curr = deque . remove ( ) ; Node currRes = resDeque . remove ( ) ; while ( curr != null ) { Node n = new Node ( curr . val , new ArrayList < > ( ) ) ; currRes . children . add ( n ) ; if ( curr . right != null ) { resDeque . add ( n ) ; deque . add ( curr . right ) ; } curr = curr . left ; } } return result ; }
Decodes your binary tree to an n-ary tree.
Decodes your binary tree to an n - ary tree .