网站注销流程wordpress保护原创
news/
2025/9/23 9:51:08/
文章来源:
网站注销流程,wordpress保护原创,wordpress在线时间代码,广州天华建筑设计有限公司文章目录1. 题目2. 解题2.1 递归2.2 BFS1. 题目
设计一个算法#xff0c;可以将 N 叉树编码为二叉树#xff0c;并能将该二叉树解码为原 N 叉树。 一个 N 叉树是指每个节点都有不超过 N 个孩子节点的有根树。 类似地#xff0c;一个二叉树是指每个节点都有不超过 2 个孩子节…
文章目录1. 题目2. 解题2.1 递归2.2 BFS1. 题目
设计一个算法可以将 N 叉树编码为二叉树并能将该二叉树解码为原 N 叉树。 一个 N 叉树是指每个节点都有不超过 N 个孩子节点的有根树。 类似地一个二叉树是指每个节点都有不超过 2 个孩子节点的有根树。 你的编码 / 解码的算法的实现没有限制你只需要保证一个 N 叉树可以编码为二叉树且该二叉树可以解码回原始 N 叉树即可。
例如你可以将下面的 3-叉 树以该种方式编码 注意上面的方法仅仅是一个例子可能可行也可能不可行。 你没有必要遵循这种形式转化你可以自己创造和实现不同的方法。
注意 N 的范围在 [1, 1000] 不要使用类成员 / 全局变量 / 静态变量来存储状态。 你的编码和解码算法应是无状态的。 来源力扣LeetCode 链接https://leetcode-cn.com/problems/encode-n-ary-tree-to-binary-tree 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 2. 解题
参考官方思路第一个子节点2接到父节点1的left其余的兄弟节点 34 都接在其左边兄弟节点的right
2.1 递归
/*
// Definition for a Node.
class Node {
public:int val;vectorNode* children;Node() {}Node(int _val) {val _val;}Node(int _val, vectorNode* _children) {val _val;children _children;}
};
*//*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Codec {
public:// Encodes an n-ary tree to a binary tree.TreeNode* encode(Node* root) {if(!root) return NULL;TreeNode* newroot new TreeNode(root-val);TreeNode* cur NULL;if(!root-children.empty()){newroot-left encode(root-children[0]);cur newroot-left;}for(int i 1; i root-children.size(); i){cur-right encode(root-children[i]);cur cur-right;}return newroot;}// Decodes your binary tree to an n-ary tree.Node* decode(TreeNode* root) {if(!root) return NULL;Node *newroot new Node(root-val);TreeNode *cur NULL;if(root-left){newroot-children.push_back(decode(root-left));cur root-left;}while(cur cur-right){newroot-children.push_back(decode(cur-right));cur cur-right;}return newroot;}
};108 ms 179.4 MB
2.2 BFS
class Codec {
public:// Encodes an n-ary tree to a binary tree.TreeNode* encode(Node* root) {if(!root) return NULL;TreeNode* newroot new TreeNode(root-val), *newTreeNode NULL;TreeNode* cur NULL;queuepairNode*, TreeNode* q;q.push({root,newroot});while(!q.empty()){int size q.size();while(size--){root q.front().first;newTreeNode q.front().second;q.pop();if(!root-children.empty()){newTreeNode-left new TreeNode(root-children[0]-val);cur newTreeNode-left;q.push({root-children[0], cur});}for(int i 1; i root-children.size(); i){cur-right new TreeNode(root-children[i]-val);;cur cur-right;q.push({root-children[i], cur});}}}return newroot;}// Decodes your binary tree to an n-ary tree.Node* decode(TreeNode* root) {if(!root) return NULL;Node *newroot new Node(root-val), *newNode NULL;Node *cur NULL;queuepairTreeNode*, Node* q;q.push({root,newroot});while(!q.empty()){int size q.size();while(size--){root q.front().first;cur q.front().second;q.pop();if(root-left){newNode new Node(root-left-val);cur-children.push_back(newNode);q.push({root-left, newNode});root root-left;while(root-right){newNode new Node(root-right-val);cur-children.push_back(newNode);q.push({root-right, newNode});root root-right;}}}}return newroot;}
};80 ms 173.6 MB 我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号Michael阿明一起加油、一起学习进步
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/912108.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!