@ww884203 I can understand your general idea. But could you elaborate on how you would actually proceed to code it up. I think it would not be easy to find the union nodes.
ayushpatwari
@ayushpatwari
Posts made by ayushpatwari

RE: Union of two binary trees (if they share common nodes)

RE: Given a (0,1) grid and coordinates (x,y) find the circumference (perimeter) of the enclosing island of 1's
@dat.vikash The problem is to find the perimeter, not the number of surrounding one's. I think drawing a diagram and marking out the island will help. Also you can check this problem which was added recently : https://leetcode.com/problems/islandperimeter/

RE: Given a (0,1) grid and coordinates (x,y) find the circumference (perimeter) of the enclosing island of 1's
@SergeyTachenov
This seems like a really good approach to me. However I am unclear why the 3rd example has circumference of 5. Shouldn't it be 13 ? Also, i was assuming that with this approach you are going to pad the entire matrix with 0's all around (or at least logically). I am not sure if you have implemented that way. But that would definitely work.Here is how I solved it:
Key Observation:
In an island how many units can a single 1 contribute ?0000 0100 0000
In this case the only 1 contributes 4 (= the actual circumference)
0000 0110 0000
In this case the each 1 contributes 3 (the actual circumference = 6)
0000 0110 0010
In this case the two 1's contributes 3 and one of them contributes 2 (the actual circumference = 8)
Lastly,
0010 0111 0010
In this case the all 1's contributes 3 and the middle one contributes 0 (the actual circumference = 12)
Now, contribution for any 1 = max degree (=4)  actual outdegree (You can confirm that from all the examples)
So basically algorithm is :
 Perform dfs/bs
 while visiting each node count its outdegree and compute contribution => add it to total circumference

RE: Given a (0,1) grid and coordinates (x,y) find the circumference (perimeter) of the enclosing island of 1's
@SergeyTachenov You are correct in understanding the coordinate system and the values. I have edited the post with the correct values! (posted in a hurry and hence the mistakes in values)

Given a (0,1) grid and coordinates (x,y) find the circumference (perimeter) of the enclosing island of 1's
E.g
01110 00110 11001 01010 (2,3) : circumference = 10 (3, 5) or (4,4) : circumference = 4 (4, 2): circumference = 8

Union of two binary trees (if they share common nodes)
e.g
Tree 1: Tree 2: A B / \ / \ B C D E /  / \ \ D ... C F G Union: A / \ B C / \  D E ... / \ \ C F G
The union of the two trees should be returned if there is an overlap. We don't know which tree might be a subtree of the other. The result should be a new tree [inputs should not be modified]
I proposed a recursive solution which would work if we knew that Tree2 could be a subtree of the other. If this is not assumed how do I go about it ?