日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode 431. 将 N 叉树编码为二叉树(递归/层序)

發布時間:2024/7/5 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 431. 将 N 叉树编码为二叉树(递归/层序) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. 題目
    • 2. 解題
      • 2.1 遞歸
      • 2.2 BFS

1. 題目

設計一個算法,可以將 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,其余的兄弟節點 3,4 都接在其左邊兄弟節點的right

2.1 遞歸

/* // Definition for a Node. class Node { public:int val;vector<Node*> children;Node() {}Node(int _val) {val = _val;}Node(int _val, vector<Node*> _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;queue<pair<Node*, 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;queue<pair<TreeNode*, 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阿明),一起加油、一起學習進步!

總結

以上是生活随笔為你收集整理的LeetCode 431. 将 N 叉树编码为二叉树(递归/层序)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。