【劍指offer】q50:樹中結點的最近祖先

#@ root: the root of searched tree
#@ nodeToFind: the tree-node to be found
#@ path: the path from root to node
#@@
#@@ search tree referenced by root, and return the path 
#@@ from root to node, if node not exist, path = []
#@@

def getPath(root, nodeToFind, path):
	if ( None == root or None == nodeToFind):
		return False
	# case 1: current root == node, so insert to path
	if root == nodeToFind:
		path.insert(0, root)
		return True 
	# search in left barch and right branch
	bFindInLeft = False
	bFindInRight = False

	if root.left:
		bFindInLeft = getPath(root.left, nodeToFind, path)

	if False == bFindInLeft and root.right :
		bFindInRight = getPath(root.right, nodeToFind, path)

	# case 2: nodeToFind in subtree of root, insert root 
	if bFindInLeft or bFindInRight:
		path.insert(0, root)
		return True

	return False

函數的功能是在root 表示的樹中查找nodeToFind 結點,若找到,則在返回的時候,將路徑結點加入到path中,關於樹的遍歷有三種,這裏我們使用後序遍歷,目的是在知道所有情況後,再對root進行處理,因爲當前的結點root應不應該加入到路徑path中,不僅跟當前的結點root有關,還跟它的子結點有關,也就是若當前結點就是要找的結點,那麼將當前結點加入是沒有問題的,但是即使當前結點不是要查找的結點,而其子樹中有查找結點時,當前結點也是要加入到路徑中去的。這樣就不用每次都將結點插入,條件不滿足時還要進行結點的pop。


def getClosetParent(root, node1, node2):
	path1 = []; path2 = []
	if None == root or None == node1 or None == node2:
		return None

	#get the path from root to node1 and node2
	getPath(root, node1, path1)
	getPath(root, node2, path2)

	# find closet parent of node1 and node2
	shorPathLen = min( len(path1), len(path2) )
	for i in range(1, shorPathLen):
		if path1[ i ] != path2[ i ] and \
			path1[ i - 1 ] == path2[ i - 1 ]:
			return path1[ i - 1 ]

	return None
因爲在getPath函數裏,我們獲得的路徑是從root開始的,即root爲path列表的第一個結點,那麼我們就從root開始,一次比較,找到最後一個相等的,就是二者最近的公共祖先。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章