|  | @@ -1,6 +1,9 @@
 | 
	
		
			
				|  |  |  package main
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import "fmt"
 | 
	
		
			
				|  |  | +import (
 | 
	
		
			
				|  |  | +	"fmt"
 | 
	
		
			
				|  |  | +	"math"
 | 
	
		
			
				|  |  | +)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  // Get the distance of two nodes in binary tree
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -8,19 +11,25 @@ var finalDist int
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func main() {
 | 
	
		
			
				|  |  |  	distOf(1, 12, 12)
 | 
	
		
			
				|  |  | -	fmt.Println(finalDist, 0)
 | 
	
		
			
				|  |  | +	fmt.Println(finalDist, 0, getDistof(12, 12))
 | 
	
		
			
				|  |  |  	distOf(1, 12, 6)
 | 
	
		
			
				|  |  | -	fmt.Println(finalDist, 1)
 | 
	
		
			
				|  |  | +	fmt.Println(finalDist, 1, getDistof(12, 6))
 | 
	
		
			
				|  |  |  	distOf(1, 4, 5)
 | 
	
		
			
				|  |  | -	fmt.Println(finalDist, 2)
 | 
	
		
			
				|  |  | +	fmt.Println(finalDist, 2, getDistof(4, 5))
 | 
	
		
			
				|  |  |  	distOf(1, 1, 11)
 | 
	
		
			
				|  |  | -	fmt.Println(finalDist, 3)
 | 
	
		
			
				|  |  | +	fmt.Println(finalDist, 3, getDistof(1, 11))
 | 
	
		
			
				|  |  |  	distOf(1, 5, 6)
 | 
	
		
			
				|  |  | -	fmt.Println(finalDist, 4)
 | 
	
		
			
				|  |  | +	fmt.Println(finalDist, 4, getDistof(5, 6))
 | 
	
		
			
				|  |  |  	distOf(1, 6, 9)
 | 
	
		
			
				|  |  | -	fmt.Println(finalDist, 5)
 | 
	
		
			
				|  |  | +	fmt.Println(finalDist, 5, getDistof(6, 9))
 | 
	
		
			
				|  |  |  	distOf(1, 8, 15)
 | 
	
		
			
				|  |  | -	fmt.Println(finalDist, 6)
 | 
	
		
			
				|  |  | +	fmt.Println(finalDist, 6, getDistof(8, 15))
 | 
	
		
			
				|  |  | +	distOf(1, 21, 13)
 | 
	
		
			
				|  |  | +	fmt.Println(finalDist, 7, getDistof(21, 13))
 | 
	
		
			
				|  |  | +	distOf(1, 101, 5)
 | 
	
		
			
				|  |  | +	fmt.Println(finalDist, 8, getDistof(101, 5))
 | 
	
		
			
				|  |  | +	distOf(1, 11, 101)
 | 
	
		
			
				|  |  | +	fmt.Println(finalDist, 9, getDistof(11, 101))
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  //           __1__
 | 
	
	
		
			
				|  | @@ -58,3 +67,29 @@ func distOf(root, p, q int) int {
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  	return -1
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func getDistof(p, q int) (dist int) {
 | 
	
		
			
				|  |  | +	lp := int(math.Log2(float64(p)))
 | 
	
		
			
				|  |  | +	lq := int(math.Log2(float64(q)))
 | 
	
		
			
				|  |  | +	if lp != lq {
 | 
	
		
			
				|  |  | +		det := abs(lp - lq)
 | 
	
		
			
				|  |  | +		dist += det
 | 
	
		
			
				|  |  | +		if lp < lq {
 | 
	
		
			
				|  |  | +			q >>= uint(det)
 | 
	
		
			
				|  |  | +		} else {
 | 
	
		
			
				|  |  | +			p >>= uint(det)
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	for ; p != q; p, q = p/2, q/2 {
 | 
	
		
			
				|  |  | +		dist += 2
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func abs(x int) int {
 | 
	
		
			
				|  |  | +	if x < 0 {
 | 
	
		
			
				|  |  | +		return -x
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return x
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 |