Amazon Interview Question
SDE-2sCountry: United States
/*
iterative : search a given node by using a queue. Once found, use a stack to iterate through the trie. Add currently visited node to the stack. pop it out, add it to the result list and add its children back to the stack in a reverse order.
time O(n) where n is total number of nodes in a trie
space O(n) in the worst case when a trie is two-level
*/
class Solution {
public List<Integer> preorder(Node root) {
List<Integer> result = new LinkedList<>();
Node target = new Node(3);
root = findNode(root, target, result);
preorderUtil(root, result);
return result;
}
private Node findNode(Node node, Node target, List<Integer> result) {
if (node.val == target.val) {
return node;
}
Queue<Node> q = new LinkedList<>();
q.add(node);
while (!q.isEmpty()) {
Node temp = q.remove();
if (temp.val == target.val) {
return temp;
}
else {
List<Node> children = temp.children;
for (Node child : children) {
q.add(child);
}
}
}
return null;
}
private void preorderUtil(Node node, List<Integer> result) {
Stack<Node> s = new Stack<>();
s.push(node);
while (!s.empty()) {
Node temp = s.pop();
result.add(temp.val);
List<Node> children = temp.children;
Stack<Node> tempS = new Stack<>();
for (Node child : children) {
tempS.push(child);
}
while (!tempS.empty()) {
s.push(tempS.pop());
}
}
return;
}
}
If we convert our n-ary representation from
class Node{
int data
List<Node>childre;
}
to :
class Node{
int data;
Node firstChild;
Node nextSibling;
}
Then, our n-ary tree becomes a simple binary tree.
We can easily traverse the tree to find the node by making use of firstChild(consider as left Pointer) and nextSibling (consider as right Pointer)
Also,preOrder Traversal can be done, following something like: root, firstChild, nextSibling.
If we convert our n-ary representation from
class Node{
int data
List<Node>childre;
}
to :
class Node{
int data;
Node firstChild;
Node nextSibling;
}
Then, our n-ary tree becomes a simple binary tree.
We can easily traverse the tree to find the node by making use of firstChild(consider as left Pointer) and nextSibling (consider as right Pointer)
Also, preOrder Traversal can be done, following something like: root, firstChild, nextSibling.
/*
- Euihoon Seol October 17, 2020recursive solution : search a given node. Once found, add a currently visited node to result and call a method itself recursively for every child of its children. Repeat the steps until it visits all the nodes.
time O(n) where n is the total number of nodes in a n-ary tree/trie.
space O(n) in the worst case when a trie is left-skewed and a call stack would have n number of method calls.
*/
class Solution {
public List<Integer> preorder(Node root) {
List<Integer> result = new LinkedList<>();
Node target = new Node(3);
root = findNode(root, target, result);
preorderUtil(root, result);
return result;
}
private Node findNode(Node node, Node target, List<Integer> result) {
if (node.val == target.val) {
return node;
}
Queue<Node> q = new LinkedList<>();
q.add(node);
while (!q.isEmpty()) {
Node temp = q.remove();
if (temp.val == target.val) {
return temp;
}
else {
List<Node> children = temp.children;
for (Node child : children) {
q.add(child);
}
}
}
return null;
}
private void preorderUtil(Node node, List<Integer> result) {
if (node == null) {
return;
}
result.add(node.val);
List<Node> children = node.children;
for (Node child : children) {
preorderUtil(child, result);
}
return;
}
}