Amazon Interview Question
Software EngineersCountry: United States
using System;
using System.Collections.Generic;
using System.Linq;
namespace test
{
public class TreeNode
{
public int val { get; set; }
public TreeNode left { get; set; }
public TreeNode right { get; set; }
public TreeNode(int x)
{
val = x;
}
}
public class FindDupNodes
{
public FindDupNodes()
{
List<TreeNode> tree = new List<TreeNode>();
tree = ConstructTree();
TreeNode node = FindMaxDups(tree);
Console.WriteLine($"{node.val} -- {node?.left?.val} -- {node?.right?.val}");
}
public List<TreeNode> ConstructTree()
{
List<TreeNode> nodes = new List<TreeNode>();
TreeNode t1 = new TreeNode(1);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(3);
TreeNode t4 = new TreeNode(4);
t1.left = t2; t1.right = t3;
t2.left = t4;
t3.left = t2;
t3.right = t4;
nodes.Add(t1);
nodes.Add(t2);
nodes.Add(t3);
nodes.Add(t4);
return nodes;
}
public TreeNode FindMaxDups(List<TreeNode> tree)
{
//get the root
TreeNode rootNode = tree.Where(t => t.val == 1).FirstOrDefault();
//iterate over in recursion and see which one appears more than once
IterateTree(rootNode, tree);
int maxValue = nodeCounts.Max(n => n.Value); //.Select(nd => nd.Key);
List<TreeNode> maxNodes = nodeCounts.Where(n => n.Value == maxValue).Select(n => n.Key).ToList();
return maxNodes.FirstOrDefault();
}
public Dictionary<TreeNode, int> nodeCounts = new Dictionary<TreeNode, int>();
public void IterateTree(TreeNode currentNode, List<TreeNode> tree)
{
if (currentNode == null)
return;
if (nodeCounts.ContainsKey(currentNode))
nodeCounts[currentNode] = nodeCounts[currentNode] + 1;
else
nodeCounts.Add(currentNode, 1);
if (currentNode.left == null && currentNode.right == null)
return;
IterateTree(currentNode.left, tree);
IterateTree(currentNode.right, tree);
}
}
}
import java.util.*;
class Main {
private static String max="";
public static void main(String[] args) {
System.out.println("Hello world!");
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(6);
root.right = new TreeNode(10);
root.right.left = new TreeNode(2);
root.right.left.left = new TreeNode(4);
root.right.left.right = new TreeNode(6);
getRepeatTree(root);
}
private static TreeNode getRepeatTree(TreeNode root)
{
Map<String,TreeNode> map = new HashMap<>();
createMap(map,root);
return map.get(max);
}
private static String createMap(Map<String,TreeNode> map,TreeNode root)
{
if(root==null)
return "";
String left = createMap(map,root.left);
String right = createMap(map,root.right);
String curr = left+right+root.val;
if(map.containsKey(curr))
{
if(max.length()<curr.length())
{
max=curr;
}
}
else
{
map.put(curr,root);
}
return curr;
}
}
class TreeNode
{
TreeNode left,right;
int val;
TreeNode(int val)
{
this.val=val;
}
}
private static TreeNode getRepeatTree(TreeNode root)
{
Map<String,TreeNode> map = new HashMap<>();
createMap(map,root);
return map.get(max);
}
private static String createMap(Map<String,TreeNode> map,TreeNode root)
{
if(root==null)
return "";
String left = createMap(map,root.left);
String right = createMap(map,root.right);
String curr = left+right+root.val;
if(map.containsKey(curr))
{
if(max.length()<curr.length())
{
max=curr;
}
}
else
{
map.put(curr,root);
}
return curr;
}
static class TreeNode {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((children == null) ? 0 : children.hashCode());
result = prime * result + data;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TreeNode other = (TreeNode) obj;
if (children == null) {
if (other.children != null)
return false;
} else if (!children.equals(other.children))
return false;
if (data != other.data)
return false;
return true;
}
TreeNode(int data) {
this.data = data;
}
@Override
public String toString() {
return this.data + "";
}
int data;
List<TreeNode> children;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
}
public static void main(String args[]) throws Exception {
Node root=new Node(2);
root.left=new Node(1);
root.right=new Node(1);
List<TreeNode> temp=findDuplicateSubtrees(root);
}
public static List<TreeNode> findDuplicateSubtrees(Node root) {
String inOrder = inOrderTraversal(root);
String preOrder = preOrderTraversal(root);
List<TreeNode> list = new ArrayList<TreeNode>();
for (int i = 0; i < inOrder.length(); i++) {
for (int j = i; j < inOrder.length(); j++) {
String subStr = inOrder.substring(i, j);
if (inOrder.indexOf(subStr) != inOrder.lastIndexOf(subStr)
&& preOrder.indexOf(subStr) != preOrder.lastIndexOf(subStr) && subStr.length()>0) {
list.add(new TreeNode(Character.getNumericValue(subStr.charAt(0)))) ;
}
}
}
return list;
}
private static String preOrderTraversal(Node root) {
if (root == null)
return "";
return root.data + preOrderTraversal(root.left) + preOrderTraversal(root.right);
}
private static String inOrderTraversal(Node root) {
if (root == null)
return "";
return inOrderTraversal(root.left) + root.data + inOrderTraversal(root.right);
}
Basically you need to find two nodes which are same to start with. And then call the isDuplicate(root1, root2) method on both. This exhaustive traversal is worst case matching of each node with each node and then isIdentical is O(n). Hence the overall complexity is O(n3)
- Yoda May 05, 2018Now any two subtrees can be duplicate only if one is not ancestor of other.