Posts

Showing posts with the label N-ary Tree

428. Serialize and Deserialize N-ary Tree

Image
https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/ https://github.com/openset/leetcode/tree/master/problems/serialize-and-deserialize-n-ary-tree https://www.lintcode.com/problem/serialize-and-deserialize-n-ary-tree/description Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize an N-ary tree. An N-ary tree is a rooted tree in which each node has no more than N children. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that an N-ary tree can be serialized to a string and this string can be deserialized to the original tree structure. For example, you may serialize the following  3-ary  tree     as  [1 [3[5 6] 2 4]] . You do n...

Symmetric N-ary Tree

Given a N-ary tree, check whether it is symmetric around its root. N-ary tree is symmetric if value and shape look the same when reflected at the root. Note  -  Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value  Example1 Input: root = [5,null,3,5,3,null,10,null, 15, null, 10]   Output: true Example2 Input: root = [5,null,3,5,3,null,10,25, null, 15, null, 10]   Output: false Example3 Input: root = [5,null,3,5,3,null,10, null, 15, null, 25, 10] Output: false Example4 Input: root = [5,null,3,5,3,null,10,25, null, 15, null, 35, 10] Output: false --- Intuition Check each level, and check if its symmetric Symmetric => Node val, and number of children at each end of level order should be the same

429. N-ary Tree Level Order Traversal

Image
https://leetcode.com/problems/n-ary-tree-level-order-traversal/ Given an n-ary tree, return the  level order  traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples). Example 1: Input: root = [1,null,3,2,4,null,5,6] Output: [[1],[3,2,4],[5,6]] Example 2: Input: root = [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],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]] Constraints: The height of the n-ary tree is less than or equal to  1000 The total number of nodes is between  [0, 10^4] --- Intuition Typical level order BFS using queue Add root to tree While Q is not Empty pop elements at current level Add child elements to end of Q ---- Time - O(n) Space - O(n) ----

590. N-ary Tree Postorder Traversal

Image
https://leetcode.com/problems/n-ary-tree-postorder-traversal/ Given an n-ary tree, return the  postorder  traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples). Follow up: Recursive solution is trivial, could you do it iteratively? Example 1: Input: root = [1,null,3,2,4,null,5,6] Output: [5,6,3,2,4,1] Example 2: Input: root = [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: [2,6,14,11,7,3,12,8,4,13,9,10,5,1] Constraints: The height of the n-ary tree is less than or equal to  1000 The total number of nodes is between  [0, 10^4] ----- Intuition Post order definition => Process the children first, and parent node after.  In recursive calls, we call into the child functions, and process the current node after called functions return. ...

589. N-ary Tree Preorder Traversal

Image
https://leetcode.com/problems/n-ary-tree-preorder-traversal/ Given an n-ary tree, return the  preorder  traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples). Follow up: Recursive solution is trivial, could you do it iteratively? Example 1: Input: root = [1,null,3,2,4,null,5,6] Output: [1,3,5,6,2,4] Example 2: Input: root = [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,2,3,6,7,11,14,4,8,12,5,9,13,10] Constraints: The height of the n-ary tree is less than or equal to  1000 The total number of nodes is between  [0, 10^4] ---- Intuition Recursive solution is trivial, just focus on iterative solution In recursion we process the node as soon as we reach it, and then recurse on its children. We can extend the same idea here - Process no...