classCodec{public:// Encodes an n-ary tree to a binary tree.TreeNode*encode(Node*root){if(root==nullptr)returnnullptr;TreeNode*rootTreeNode=newTreeNode(root->val);queue<pair<Node*,TreeNode*>>q{{{root,rootTreeNode}}};while(!q.empty()){constauto[parentNode,parentTreeNode]=q.front();q.pop();TreeNode*prevTreeNode=nullptr;TreeNode*headTreeNode=nullptr;for(Node*child:parentNode->children){TreeNode*currTreeNode=newTreeNode(child->val);if(prevTreeNode!=nullptr)prevTreeNode->right=currTreeNode;elseheadTreeNode=currTreeNode;prevTreeNode=currTreeNode;q.emplace(child,currTreeNode);}parentTreeNode->left=headTreeNode;}returnrootTreeNode;}// Decodes your binary tree to an n-ary tree.Node*decode(TreeNode*root){if(root==nullptr)returnnullptr;Node*rootNode=newNode(root->val);queue<pair<Node*,TreeNode*>>q{{{rootNode,root}}};while(!q.empty()){constauto[parentNode,parentTreeNode]=q.front();q.pop();TreeNode*sibling=parentTreeNode->left;while(sibling){Node*currNode=newNode(sibling->val);parentNode->children.push_back(currNode);q.emplace(currNode,sibling);sibling=sibling->right;}}returnrootNode;}};
classCodec{// Encodes an n-ary tree to a binary tree.publicTreeNodeencode(Noderoot){if(root==null)returnnull;TreeNoderootTreeNode=newTreeNode(root.val);Queue<Pair<Node,TreeNode>>q=newArrayDeque<>(Arrays.asList(newPair<>(root,rootTreeNode)));while(!q.isEmpty()){NodeparentNode=q.peek().getKey();TreeNodeparentTreeNode=q.poll().getValue();TreeNodeprevTreeNode=null;TreeNodeheadTreeNode=null;for(Nodechild:parentNode.children){TreeNodecurrTreeNode=newTreeNode(child.val);if(prevTreeNode==null)headTreeNode=currTreeNode;elseprevTreeNode.right=currTreeNode;prevTreeNode=currTreeNode;q.offer(newPair<>(child,currTreeNode));}parentTreeNode.left=headTreeNode;}returnrootTreeNode;}// Decodes your binary tree to an n-ary tree.publicNodedecode(TreeNoderoot){if(root==null)returnnull;NoderootNode=newNode(root.val,newArrayList<>());Queue<Pair<Node,TreeNode>>q=newArrayDeque<>(Arrays.asList(newPair<>(rootNode,root)));while(!q.isEmpty()){NodeparentNode=q.peek().getKey();TreeNodeparentTreeNode=q.poll().getValue();TreeNodesibling=parentTreeNode.left;while(sibling!=null){NodecurrNode=newNode(sibling.val,newArrayList<>());parentNode.children.add(currNode);q.offer(newPair<>(currNode,sibling));sibling=sibling.right;}}returnrootNode;}}
classCodec:# Encodes an n-ary tree to a binary tree.defencode(self,root:'Node')->Optional[TreeNode]:ifnotroot:returnNonerootTreeNode=TreeNode(root.val)q=deque([(root,rootTreeNode)])whileq:parentNode,parentTreeNode=q.popleft()prevTreeNode=NoneheadTreeNode=NoneforchildinparentNode.children:currTreeNode=TreeNode(child.val)ifprevTreeNode:prevTreeNode.right=currTreeNodeelse:headTreeNode=currTreeNodeprevTreeNode=currTreeNodeq.append((child,currTreeNode))parentTreeNode.left=headTreeNodereturnrootTreeNode# Decodes your binary tree to an n-ary tree.defdecode(self,root:Optional[TreeNode])->'Node':ifnotroot:returnNonerootNode=Node(root.val,[])q=deque([(rootNode,root)])whileq:parentNode,parentTreeNode=q.popleft()sibling=parentTreeNode.leftwhilesibling:currNode=Node(sibling.val,[])parentNode.children.append(currNode)q.append((currNode,sibling))sibling=sibling.rightreturnrootNode