返回介绍

solution / 1500-1599 / 1506.Find Root of N-Ary Tree / README_EN

发布于 2024-06-17 01:03:18 字数 6387 浏览 0 评论 0 收藏 0

1506. Find Root of N-Ary Tree

中文文档

Description

You are given all the nodes of an N-ary tree as an array of Node objects, where each node has a unique value.

Return _the root of the N-ary tree_.

Custom testing:

An N-ary tree can be serialized as represented in its level order traversal where 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].

The testing will be done in the following way:

  1. The input data should be provided as a serialization of the tree.
  2. The driver code will construct the tree from the serialized input data and put each Node object into an array in an arbitrary order.
  3. The driver code will pass the array to findRoot, and your function should find and return the root Node object in the array.
  4. The driver code will take the returned Node object and serialize it. If the serialized value and the input data are the same, the test passes.

 

Example 1:

Input: tree = [1,null,3,2,4,null,5,6]
Output: [1,null,3,2,4,null,5,6]
Explanation: The tree from the input data is shown above.
The driver code creates the tree and gives findRoot the Node objects in an arbitrary order.
For example, the passed array could be [Node(5),Node(4),Node(3),Node(6),Node(2),Node(1)] or [Node(2),Node(6),Node(1),Node(3),Node(5),Node(4)].
The findRoot function should return the root Node(1), and the driver code will serialize it and compare with the input data.
The input data and serialized Node(1) are the same, so the test passes.

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 * 104].
  • Each node has a unique value.

 

Follow up:

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

Solutions

Solution 1

"""
# Definition for a Node.
class Node:
  def __init__(self, val=None, children=None):
    self.val = val
    self.children = children if children is not None else []
"""


class Solution:
  def findRoot(self, tree: List['Node']) -> 'Node':
    x = 0
    for node in tree:
      x ^= node.val
      for child in node.children:
        x ^= child.val
    return next(node for node in tree if node.val == x)
/*
// Definition for a Node.
class Node {
  public int val;
  public List<Node> children;


  public Node() {
    children = new ArrayList<Node>();
  }

  public Node(int _val) {
    val = _val;
    children = new ArrayList<Node>();
  }

  public Node(int _val,ArrayList<Node> _children) {
    val = _val;
    children = _children;
  }
};
*/

class Solution {
  public Node findRoot(List<Node> tree) {
    int x = 0;
    for (Node node : tree) {
      x ^= node.val;
      for (Node child : node.children) {
        x ^= child.val;
      }
    }
    for (int i = 0;; ++i) {
      if (tree.get(i).val == x) {
        return tree.get(i);
      }
    }
  }
}
/*
// 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 x = 0;
    for (Node* node : tree) {
      x ^= node->val;
      for (Node* child : node->children) {
        x ^= child->val;
      }
    }
    for (int i = 0;; ++i) {
      if (tree[i]->val == x) {
        return tree[i];
      }
    }
  }
};
/**
 * Definition for a Node.
 * type Node struct {
 *   Val int
 *   Children []*Node
 * }
 */

func findRoot(tree []*Node) *Node {
  x := 0
  for _, node := range tree {
    x ^= node.Val
    for _, child := range node.Children {
      x ^= child.Val
    }
  }
  for i := 0; ; i++ {
    if tree[i].Val == x {
      return tree[i]
    }
  }
}
/**
 * Definition for Node.
 * class Node {
 *   val: number
 *   children: Node[]
 *   constructor(val?: number, children?: Node[]) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.children = (children===undefined ? [] : children)
 *   }
 * }
 */

function findRoot(tree: Node[]): Node | null {
  let x = 0;
  for (const node of tree) {
    x ^= node.val;
    for (const child of node.children) {
      x ^= child.val;
    }
  }
  return tree.find(node => node.val === x) || null;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文