1. 程式人生 > >LeetCode 二叉樹路徑問題 Path SUM(①②③)總結

LeetCode 二叉樹路徑問題 Path SUM(①②③)總結

題目一:Path Sum


題目大意是這個意思,給定一棵二叉樹和一個sum值,判斷樹中是否存在一條從根節點到葉子節點的路徑,使得路徑上的值加起來剛好等於sum。

解題思路:

遞迴結束條件:

root == null返回false,表示不存在;

root.left == null && root.right == null && sum - root.val == 0 ;返回true,表示找到了路徑

遞迴過程:

依次從左子樹和右子樹中查詢,注意sum = sum - root.val

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null) 
            return false;
        if(root.left == null && root.right == null && sum - root.val == 0) 
            return true;
        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    }
}


題目二:Path Sum 2

相關推薦

LeetCode 路徑問題 Path SUM①②③總結

題目一:Path Sum題目大意是這個意思,給定一棵二叉樹和一個sum值,判斷樹中是否存在一條從根節點到葉子節點的路徑,使得路徑上的值加起來剛好等於sum。解題思路:遞迴結束條件:root == nul

LeetCode 124. Binary Tree Maximum Path Sum最大路徑

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node

leetcode 112. Path SumC語言,,遞迴思想28

貼原題: Given a binary tree and a sum, determine if the tree has aroot-to-leaf path such that adding

LeetCode 257. Binary Tree Paths 路徑

res owin arr nod def 所有 fun href binary Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree:

112. Path Sum路徑

aps exist display splay term post ase urn 數據結構 [抄題]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such th

LeetCode 124. Binary Tree Maximum Path Sum中最長路徑和,遞迴

Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node t

LeetCode 145 Binary Tree Postorder Traversal的興許遍歷+、叠代

int truct fin for data- right class span popu 翻譯 給定一個二叉樹。返回其興許遍歷的節點的值。 比如: 給定二叉樹為 {1。 #, 2, 3} 1 2 / 3 返回

LeetCode最小深度簡單

給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \

LeetCode最大深度簡單

問題描述: 給定一個二叉樹,找出其最大深度。 二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20

LeetCode判斷是否相同簡單

問題描述: 給定兩個二叉樹,編寫一個函式來檢驗它們是否相同。 如果兩個樹在結構上相同,並且節點具有相同的值,則認為它們是相同的。 示例 1: 輸入: 1 1 / \ / \ 2 3 2

[leetcode]257. Binary Tree Paths路徑

Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example: Input: 1 / \ 2 3 \ 5 Output: [

Leetcode 103 的鋸齒形層次遍歷 的層次遍歷

給定一個二叉樹,返回其節點值的鋸齒形層次遍歷。(即先從左往右,再從右往左進行下一層遍歷,以此類推,層與層之間交替進行)。 例如: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回鋸

[LeetCode] Binary Tree Paths 路徑

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree:   1 / \ 2 3 \ 5 All root-

最大深度遞迴實現python---LeetCode

# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None #

LeetCode 路徑遍歷

LeetCode 112. 路徑總和 題目描述: 給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上所有節點值相加等於目標和。 示例: 給定如下二叉樹,以及目標和 sum = 22, 5

LeetCode:102. Binary Tree Level Order Traversal的層次遍歷

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given b

Leetcode 103 的鋸齒形層次遍歷 的層次遍歷

給定一個二叉樹,返回其節點值的鋸齒形層次遍歷。(即先從左往右,再從右往左進行下一層遍歷,以此類推,層與層之間交替進行)。 例如: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7

LeetCode:236. Lowest Common Ancestor of a Binary Tree中最小祖先

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “Th

C語言強化求和為某個值的路徑

遞迴究竟有多強大,看看這道題就知道了。 通過這道題,你可以掌握 如何使用遞迴遞迴的本質如何跳出遞迴死迴圈 題目:輸入一個整數和一棵二元樹。 從樹的【根結點】開始往下訪問一直到【葉結點】所經過的所有結點形成一條路徑。 打印出和與輸入整數相等的所有路徑。 例如,輸入20

leetcode-的前中後遍歷94、144、145

中序遍歷 中序遍歷的順序:左中右 思路 遞迴 每次遞迴,只需要判斷結點是不是None,否則按照左中右的順序打印出結點value值 class Solution: def inorderTraversal(self, root): ""