【LeetCode】112. 路徑總和
給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上所有節點值相加等於目標和。
說明: 葉子節點是指沒有子節點的節點。
示例:
給定如下二叉樹,以及目標和 sum = 22
,
5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
返回 true
, 因為存在目標和為 22 的根節點到葉子節點的路徑 5->4->11->2
。
#!/usr/bin/python3 # -*- coding: utf-8 -*- # @Time: 2018/7/31 # @Author: xfLi # The file... class TreeNode(object): def __init__(self, val=None, left=None, right=None): self.val = val self.left = left self.right = right def hasPathSum(root, sum): """ :type root: TreeNode :type sum: int :rtype: bool """ if not root: return False if sum == root.val and not root.left and not root.right: return True target = sum - root.val #在下個子樹找的目標值 return hasPathSum(root.left, target) or hasPathSum(root.right, target) if __name__ == '__main__': root = TreeNode(val=1, left=TreeNode(2, left=TreeNode(3), right=TreeNode(4)), right=TreeNode(2, left=TreeNode(3), right=TreeNode(4))) sum = 6 result = hasPathSum(root, sum) print(result)
相關推薦
【LeetCode】112. 路徑總和
給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上所有節點值相加等於目標和。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定如下二叉樹,以及目標和 sum = 22, 5 / \
【LeetCode】112.Sqrt(x)
題目描述(Easy) Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative inte
【LeetCode】216. 組合總和 III 結題報告 (C++)
原題地址:https://leetcode-cn.com/problems/combination-sum-iii/submissions/ 題目描述: 找出所有相加之和為 n 的 k 個數的組合。組合中只允許含有 1 - 9 的正整數,並且每種組合中不存在重複的數字。 說明: 所有
【LeetCode】112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the
【leetcode】112.Path Sum
題目描述 判斷一棵樹中有沒有一條路徑,其和等於給定值。 思路 用深度優先搜尋(DFS)遍歷樹的所有結點的值,要注意每深一層要從中減去相應節點的值。 程式碼 # Definition for a binary tree node. # class TreeNode
【LeetCode】39. 組合總和
題目描述 給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重複被選取。 說明: 所有數字(包括 target)都是正整數。
【LeetCode】40. 組合總和 II 結題報告 (C++)
題目描述: 給定一個數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的每個數字在每個組合中只能使用一次。 說明: 所有數字(包括目標數)都是正整數。
【LeetCode】39. 組合總和 結題報告 (C++)
題目描述: 給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重複被選取。 說明: 所有數字(包括 target)都是
【leetcode】Python實現-112.路徑總和
描述 我 class Solution: def hasPathSum(self, root, sum): """ :type root: TreeNode :type sum: int
【Leetcode】【DFS】 112. Path Sum / 路經總和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
【LeetCode 簡單題】97-路徑總和III
宣告: 今天是第97道題。給定一個二叉樹,它的每個結點都存放著一個整數值,找出路徑和等於給定數值的路徑總數。以下所有程式碼經過樓主驗證都能在LeetCode上執行成功,程式碼也是借鑑別人的,在文末會附上參考的部落格連結,如果侵犯了博主的相關權益,請聯絡我刪除 (手動比心ღ( ´・ᴗ・` ))
【LeetCode】Path Sum(路徑總和)
這道題是LeetCode裡的第112道題。是我在學資料結構——二叉樹的時候碰見的題。 題目要求: 給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上所有節點值相加等於目標和。 說明: 葉子節點是指沒有子節點的節點。 示例: 
【LeetCode 中等題】53-路徑總和II
題目描述:給定一個二叉樹和一個目標和,找到所有從根節點到葉子節點路徑總和等於給定目標和的路徑。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定如下二叉樹,以及目標和 sum = 22, 5 / \
leetcode 112. 路徑總和
nbsp spa true lse style 相加 HA color 葉子節點 給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上所有節點值相加等於目標和。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定如下二叉樹,以及目標和 sum
【Leetcode】【DP-二維陣列】 63. Unique Paths II / 不同路徑2(帶障礙)
給定一個二維陣列,每格為0/1值,1代表無法通過。求從左上到右下的不同路徑數。只能往右/下走。 Input: [ [0,0,0], [0,1,0], [0,0,0] ] Output: 2 Explanation: There is one obstacl
【Leetcode】【DP-二維陣列】 62. Unique Paths / 不同路徑
給一個形狀為m x n的矩陣,求從左上角到右下角的不同路徑的個數。行進時只能往右/下移動。 方法一:使用二維陣列儲存每個位置的dp值 稍作畫圖分析即可得到dp式子:dp [i] [j] = dp [i-1] [j] + dp [i] [j-1] (
【Leetcode】【DP-二維陣列】 64. Minimum Path Sum / 最小路徑和】
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum
Leetcode:112.路徑總和
給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上所有節點值相加等於目標和。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定如下二叉樹,以及目標和 sum = 22, 5
【LeetCode】字串 string(共112題)
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } 【3】Longest Substring Without Repeating Characters 【5】Longest Palindromic Substring
LeetCode——112.路徑總和(JavaScript)
給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上所有節點值相加等於目標和。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定如下二叉樹,以及目標和 sum = 22, 5 / \