Network transmission of a binary tree with minimal space

I recently came across an interview question posed by Amazon:

For a binary tree to be transmitted over the network. How to transfer this tree to the minimum space ?


Ok, my 2 approaches to the above question:

  • We can store the order tree in an array along with pre-order (or after-order or level order) in another array, and then transfer 2 arrays to the network. It takes up a lot of space. Therefore, rejecting this, I came up with a different solution.

  • We will go through each tree-level node along with some information about this left and right child elements.

Method 2

Additional information with nodes:

if the left child == NULL && & right child == NULL pass 00 together with node

if the left child! = NULL && right child == NULL pass 10 together with node

if left child == NULL && & right child! = NULL pass 01 along with node

if the left child! = NULL && the right child! = NULL pass 11 together with node

Let's look at an example for the second method

enter image description here

Level of wisdom

  • Pass node (2) and 11
  • Pass node (7) and 11
  • Pass node (5) and 11
  • Pass node (2) and 00
  • Pass node (6) and 11
  • Pass node (9) and 10
  • Pass node (5) and 00
  • Pass node (11) and 00
  • Pass node (4) and 00

, , ( , ), , node , .

, 2 :

  • 2 - , , , .
  • , 2, ?
+4
1

, , , . , , - .

. u , ( ) . ..   1  /     \ 2     3 , , 1 2 # # 3 # #... , ., , , (.. ), "#". , , . u , , - , . 1. -, 2 , u , ( , . U ). node u -, . 2. u , , o () . , , , . .
+2

Source: https://habr.com/ru/post/1548281/


All Articles