Problem statement: Given two trees how do we check whether the trees are isomorphic or not !!
Two binary trees root1 & root2 are isomorphic if they are the same structure. The values of nodes does not affect whether two trees are isomorphic or not.
Two binary trees root1 & root2 are isomorphic if they are the same structure. The values of nodes does not affect whether two trees are isomorphic or not.
- int IsIsometric(TreeNode root1, TreeRoot root2){
- f(root1 == null && root2 == null)
- return 1;
- if((root1 == null && root2 != null) || (root1 !=null && root2 == null)) return 0;
- return (IsIsometric(root1.getLeft(), root2.getLeft()) && IsIsometric(root1.getRight(), root2.getRight()));
- }
Time complexity: O(n), Space complexity: O(n)
No comments:
Post a Comment