求二叉樹中任意兩個結點的距離

2022-01-23 03:51:31 字數 4152 閱讀 2925

實現步驟:

計算跟到第乙個結點的距離;

計算跟到第二個結點的距離;

計算lca;

計算跟到lca結點的距離;

結果為(1) + (2) - 2 * (4),因為重複計算了兩次的從跟到lca結點的距離;

class

node

(object

):def

__init__

(self

,value=0

):self

.value

=value

self

.left

=self

.right

=none

defget_path_length

(root,n

,path

):if

notroot

:return0if

root

.value==n

:return

path

else

:return

get_path_length

(root

.left,n

,path+1

)orget_path_length

(root

.right,n

,path+1

)def

find_lca

(root,n1

,n2):if

root

isnone

:return

none

ifroot

.value

==n1

orroot

.value

==n2

:return

root

left

=find_lca

(root

.left,n1

,n2)right

=find_lca

(root

.right,n1

,n2)if

left

andright

:return

root

elif

left

:return

left

elif

right

:return

right

else

:return

none

deffind_distance

(root,n1

,n2):x

=get_path_length

(root,n1

,0)y

=get_path_length

(root,n2

,0)lca

=find_lca

(root,n1

,n2).

value

lca_dis

=get_path_length

(root

,lca,0

)return(x

+y)-

2*lca_dis

defget_path_length

(root

,path,k

):# base case handling

ifroot

isnone

:return

false

path.(

root

.value)if

root

.value==k

:return

true

if((

root

.left

!=none

andget_path_length

(root

.left

,path,k

))or

(root

.right

!=none

andget_path_length

(root

.right

,path,k

))):

return

true

# 如果當前結點的值並不是k

path

.pop

()return

false

deffind_distance

(root,n1

,n2):if

root

:# 獲取第乙個結點的路徑(儲存跟結點到i)

path1=

get_path_length

(root

,path1,n1

)# 獲取第二個結點的路徑

path2=

get_path_length

(root

,path2,n2

)# 找到它們的公共祖先i=

0while

i<

len(

path1

)and

i<

len(

path2

):if

path1[i

]!=path2[i

]:breaki=

i+1# 減去重複計算的跟結點到lca部分即為結果

return

(len

(path1)+

len(

path2)-

2*i)

else

:return

0

if__name__

=='__main__'

:root

=node(1

)root

.left

=node(2

)root

.right

=node(3

)root

.left

.left

=node(4

)root

.right

.right

=node(7

)root

.right

.left

=node(6

)root

.left

.right

=node(5

)root

.right

.left

.right

=node(8

)dist

=find_distance

(root,4

,5)print

("distance between node {} & {}: {}"

.format(4

,5,dist

))dist

=find_distance

(root,4

,6)print

("distance between node {} & {}: {}"

.format(4

,6,dist

))dist

=find_distance

(root,3

,4)print

("distance between node {} & {}: {}"

.format(3

,4,dist

))dist

=find_distance

(root,2

,4)print

("distance between node {} & {}: {}"

.format(2

,4,dist

))dist

=find_distance

(root,8

,5)print

("distance between node {} & {}: {}"

.format(8

,5,dist

))

結果為:

distance between node 4 & 5: 2

distance between node 4 & 6: 4

distance between node 3 & 4: 3

distance between node 2 & 4: 1

distance between node 8 & 5: 5

求二叉樹中任意兩結點的距離

與該題的一道相似題為 求二叉樹中結點的最長距離。兩題看似有聯絡,但是做法不同。分析 距離和深度之間存在必然聯絡。如果已知最長距離的兩結點的最低公共根節點r 那麼我們求r 的左右子樹深度相加即最長距離求出。如下圖所示 我們發現a和b是最長距離,他們的最低公共根節點為c,假如我們知道c的左右子樹高度均為...

求二叉樹中任意兩個結點間的路徑(C )

include include using namespace std struct node void getnodepath node root,node node,vector v,bool flag 用後根遍歷的方式尋找node,找到後儲存從該節點到根節點的路徑 node creattree...

求乙個二叉樹中任意兩個節點間的距離,

網易有道筆試 1 求乙個二叉樹中任意兩個節點間的距離,兩個節點的距離的定義是 這兩個節點間邊的個數,比如某個孩子節點和父節點間的距離是1,和相鄰兄弟節點間的距離是2,優化時間空間複雜度。c codes as below using system namespace node i2 new node ...