文章目录

  • 1. 题目
  • 2. 解题

1. 题目

Given all the nodes of an N-ary tree as an array Node[] tree where each node has a unique value.

Find and return the root of the N-ary tree.

Follow up:

Could you solve this problem in constant space complexity with a linear time algorithm?

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

For example, the above tree is serialized as [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14].

Custom testing:
You should provide the serialization of the input tree.
The Driver code then extracts the nodes from the tree and shuffles them.
You shouldn’t care how the extracted nodes are shuffled.
The driver code will provide you with an array of the extracted nodes in random order and you need to find the root of the tree out of these nodes.

Example 1:

Input: tree = [1,null,3,2,4,null,5,6]
Output: [1,null,3,2,4,null,5,6]
Explanation: The input tree is shown above.
The driver code first extracts the nodes so we now have an array of all tree nodes [Node(1),Node(3),Node(2),Node(4),Node(5),Node(6)],
then the array is randomly shuffled, thus the actual input is [Node(5),Node(4),Node(3),Node(6),Node(2),Node(1)].
The root of the tree is Node(1) and the output is the serialization of the node you return.

Example 2:

Input: tree = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]

Constraints:
The total number of nodes is between [1, 5 * 10^4].
Each node has a unique value.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-root-of-n-ary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 把每个节点及其直接连接的子节点的值进行异或,题目说值无重复
  • 这样根节点只运算了1次,其余节点运算了2次(异或偶数次抵消了)
  • 最后遍历所有的节点,找到 val 等于异或值的就是根节点
/*
// 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;}
};
*/class Solution {public:Node* findRoot(vector<Node*> tree) {int XOR = 0;for(Node* root : tree) {XOR ^= root->val;for(Node* child : root->children)XOR ^= child->val;}for(Node* root : tree){if(XOR == root->val)return root;}return NULL;}
};

248 ms 252.6 MB


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!

LeetCode 1506. Find Root of N-Ary Tree(异或)相关推荐

  1. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  2. 【同113】LeetCode 129. Sum Root to Leaf Numbers

    LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...

  3. 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  4. Leetcode 742. Closest Leaf in a Binary Tree

    Leetcode 742. Closest Leaf in a Binary Tree Approach #1: Convert to Graph 把树转换为以k节点为中心的图,从k节点开始层序遍历, ...

  5. LeetCode: 111. Minimum Depth of Binary Tree

    题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...

  6. leetcode 1339. Maximum Product of Splitted Binary Tree | 1339. 分裂二叉树的最大乘积(树形dp)

    题目 https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/ 题解 树形 dp,思路见草稿 /*** Defini ...

  7. leetcode -- 515. Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1          / \     ...

  8. leetcode python3 简单题226. Invert Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十六题 (1)题目 英文: Invert a binary tree. 中文 ...

  9. leetcode python3 简单题110. Balanced Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十题 (1)题目 英文: Given a binary tree, dete ...

最新文章

  1. 效率提升一倍,成本下降 80%,阿里云落地全球最大规模云原生实践
  2. 作业帮产品负责人傅弋铮:在线教育产品还要攻克多少难关?
  3. linux--监控系统之Zabbix简介(二)
  4. hadoop的安装与配置(伪分布式模式安装)
  5. java cmd停服务_java代码启动cmd执行命令来开启服务出现无法连接的问题
  6. JAVA EE配TOMCAT
  7. 接收机端口电压dBuV如何转化为功率dBmW
  8. 流水作业c语言代码,C语言流水灯
  9. 怎么用QQ查看对方IP
  10. Android升级WebView浏览器内核版本
  11. 2021前端经典面试题
  12. 钉钉群机器人通知中的图片,突然不显示了
  13. ET框架学习——消息系统之二
  14. 如何以活动价在官网购买百度网盘会员
  15. What is tethering and how do you enable tethering?
  16. hibernate之多对多映射操作中间表
  17. 网络安全自学入门:(超详细)从入门到精通学习路线规划,学完即可就业
  18. 显示器接口_显示器带全能TypeC接口用起来有多爽?
  19. idea 启动项目找不到程序包,提示程序包不存在
  20. 蓝色基因超级计算机玩仙剑6,世界上最快的计算机——蓝色基因超级计算机

热门文章

  1. python命令方式和关键字
  2. np.random.seed(0)作用
  3. jquery post php返回html,jquery ajax post 提交数据,返回的是当前网页的html?
  4. php 匹配双字节字符串,收集一些常用的正则表达式(匹配中文字符、匹配双字节字符、匹配HTML标记、匹配空行 and so on~~~)...
  5. Codeforces - 1194C - From S To T - 子序列 - 排序
  6. C#学习记录3上——类的封装,继承,多态
  7. 随感 -- 2013/08/16
  8. 【转】[iOS] 关于 self = [super init];
  9. flare3d_clone
  10. MySQL源码解读之数据结构-LF_DYNARRAY