劍指Offer 34 二叉樹中和為某一值的路徑

2021-09-09 05:21:40 字數 3964 閱讀 4442

輸入一棵二叉樹和乙個整數,列印出二叉樹中節點值的和為輸入整數的所有路徑。

從樹的根節點開始往下一直到葉節點所經過的節點形成一條路徑。

例:給定如下二叉樹,以及目標和 sum = 22,

5

/ \4 8

/ / \

11 13 4

/ \ / \

7 2 5 1

返回:

[[5,4,11,2],

[5,8,4,5]

]

思路很明確,深度優先搜尋sum(node) = sum(node.left) + node.val or sum(node.right) + node.val。書上要求列印,因此只要在記錄路徑,在葉節點比較路徑和目標,如果相等則列印即可。但為了在leetcode上測試,我實現的是返回包含所有符合條件路徑的列表,為了不使用全域性變數,路徑節點的值在返回時新增。如果使用全域性的path列表來儲存合規路徑,更加簡單。

時間複雜度:o(n)

空間複雜度:列印:o(n) 返回列表:o(nlogn)

def

path_in_tree

(root,

sum)

:"""

:type root: treenode

:type sum: int

:rtype: list[list[int]]

"""defrecursion_core

(node,

sum)

:"""

:param node:subtree root

:param sum: sub sum

:return: [path(list)] if has path else

"""ifnot node.left and

not node.right:

subpath =[[

]]ifsum

== node.val else

else

: subpath_l = recursion_core(node.left,

sum- node.val)

if node.left else

subpath_r = recursion_core(node.right,

sum- node.val)

if node.right else

subpath = subpath_l + subpath_r

return

[[node.val]

+ path for path in subpath]

# add path from leaf to root

ifnot root:

return

return

print

(recursion_core(root,

sum)

)

這題目思路很簡單,但是實現的時候我還是遇到了點麻煩,就是處理返回值的時候怎麼標記錯誤路徑。畫圖推理了下,就很清晰了。當出現錯誤路徑時,leaf節點返回的是,將被列表加法和迴圈忽略,只有當[ ]中有值時才向其中每個元素新增node.val。

還有一點時在處理遞迴的結束條件時,一開始我在node = none時處理,結果返回值中所有路徑重複了2次。是因為leaf.left leaf.right都是合規路徑,各自返回了,被葉節點重複處理了。因此改為在leaf處終結遞迴。

如果不是列印的話,最深遞迴層次o(n),但是此時最多只有一條路徑,最差情況出現在,完美二叉樹時,所有路徑都是符合條件的,路徑長度logn,路徑數目n/2,o(nlogn)

# definition for a binary tree node.

# class treenode:

# def __init__(self, x):

# self.val = x

# self.left = none

# self.right = none

class

solution

:def

pathsum

(self, root,

sum)

:"""

:type root: treenode

:type sum: int

:rtype: list[list[int]]

"""defrecursion_core

(node,

sum):if

not node.left and

not node.right:

subpath =[[

]]ifsum

== node.val else

else

: subpath_l = recursion_core(node.left,

sum- node.val)

if node.left else

subpath_r = recursion_core(node.right,

sum- node.val)

if node.right else

subpath = subpath_l + subpath_r

return

[[node.val]

+ path for path in subpath]

ifnot root:

return

return recursion_core(root,

sum)

# return list not print

給定乙個二叉樹和乙個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上所有節點值相加等於目標和。

說明: 葉子節點是指沒有子節點的節點。

示例:

給定如下二叉樹,以及目標和 sum = 22,

5

/ \4 8

/ / \

11 13 4

/ \ \

7 2 1

返回 true, 因為存在目標和為 22 的根節點到葉子節點的路徑 5->4->11->2。

definition for a binary tree node.

# class treenode:

# def __init__(self, x):

# self.val = x

# self.left = none

# self.right = none

class

solution

:def

haspathsum

(self, root,

sum)

:"""

:type root: treenode

:type sum: int

:rtype: bool

"""defrecursion

(node,

sum, target):if

not node:

return

false

elif

not node.left and

not node.right:

return

sum+ node.val == target

else

:return recursion(node.right,

sum+ node.val, target)

or recursion(node.left,

sum+ node.val, target)

return recursion(root,0,

sum)

劍指Offer 34 二叉樹中和為某一值的路徑

題目描述 輸入一顆二叉樹和乙個整數,列印出二叉樹中結點值的和為輸入整數的所有路徑。路徑定義為從樹的根結點開始往下一直到葉結點所經過的結點形成一條路徑。struct treenode class solution void dfs treenode root,int s,vector ret,vect...

劍指Offer 34 二叉樹中和為某一值的路徑

20.5.3 最佳寫法 注意不要在新增了一條路徑後直接返回,任何乙個節點返回時都要從路徑中pop出去 class solution void dfs treenode root,int sum if root left dfs root left,sum if root right dfs root...

劍指offer34 二叉樹中和為某一值的路徑

1.判斷是不是空樹 2.遞迴結束的條件是葉子節點 3.考察二叉樹前序遍歷 coding utf 8 class treenode def init self,x self.val x self.left none self.right none class solution 返回二維列表,內部每個列...