Saturday, 15 August 2015

data structures - Find two Binary Tree are structurally identical -



data structures - Find two Binary Tree are structurally identical -

i asked in interview

what efficient algorithm find if 2 given binary trees structurally identical not content?

/ \ b c \ e z / \ u v \ t

are structurally identical.

next question find out if 2 binary tree structurally mirror ?

any pointer or help appreciated.

my effort was

boolean isstrucutrallyidentitical(binarynode root1, binaynode root2) { if(root1==null && root2==null) homecoming true; if(root1==null || root2==null) homecoming false; if(root1!=null && roo2!=null) homecoming true; // instead of value check if null or not homecoming isstrucutrallyidentitical(root1.getleft(), root2.getleft()) && isstrucutrallyidentitical(root1.getright(), root2.getright()); }

public boolean arestructuralysame(treenode<integer> tree1, treenode<integer> tree2) { if(tree1 == null && tree2 == null) { homecoming true; } if(tree1 == null || tree2 == null) { homecoming false; } else homecoming (arestructuralysame(tree1.getleft(), tree2.getleft()) && arestructuralysame(tree1.getright(), tree2.getright())); }

this works fine

data-structures binary-tree jaas

No comments:

Post a Comment