摘要:
112. Path Sum Problem
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.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22,
5 / 4 8 / / 11 13 4 / 7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
Solution Recursiveclass Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; //has to be a root-to-leaf path if (root.val == sum && root.left == null && root.right == null) return true; return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val); } }Update 2018-11 Solution Iterative
class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; DequePath Sum II Problemnodestack = new ArrayDeque<>(); Deque sumstack = new ArrayDeque<>(); nodestack.push(root); sumstack.push(sum); while (!nodestack.isEmpty()) { TreeNode curNode = nodestack.pop(); int curSum = sumstack.pop(); if (curNode.left == null && curNode.right == null && curNode.val == curSum) return true; if (curNode.right != null) { nodestack.push(curNode.right); sumstack.push(curSum-curNode.val); } if (curNode.left != null) { nodestack.push(curNode.left); sumstack.push(curSum-curNode.val); } } return false; } }
Given a binary tree and a sum, find all root-to-leaf paths where each path"s sum equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22,
5 / 4 8 / / 11 13 4 / / 7 2 5 1
Return:
[ [5,4,11,2], [5,8,4,5] ]Solution
class Solution { public ListPath Sum III Problem> pathSum(TreeNode root, int sum) { List
> res = new ArrayList<>(); if (root == null) return res; dfs(root, sum, new ArrayList
(), res); return res; } private void dfs(TreeNode root, int sum, List temp, List > res) { if (sum == root.val && root.left == null && root.right == null) { temp.add(root.val); List
save = new ArrayList<>(temp); //important res.add(save); temp.remove(temp.size()-1); } else { temp.add(root.val); if (root.left != null) dfs(root.left, sum-root.val, temp, res); if (root.right != null) dfs(root.right, sum-root.val, temp, res); temp.remove(temp.size()-1); } } }
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
Example:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
10 / 5 -3 / 3 2 11 / 3 -2 1
Return 3. The paths that sum to 8 are:
5 -> 3
5 -> 2 -> 1
-3 -> 11
Solutionclass Solution { public int pathSum(TreeNode root, int sum) { if (root == null) return 0; return helper(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); } private int helper(TreeNode root, int sum) { int count = 0; if (sum == root.val) count++; if (root.left != null) count += helper(root.left, sum-root.val); if (root.right != null) count += helper(root.right, sum-root.val); return count; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/77098.html
摘要:只要我們能夠有一個以某一中間路徑和為的哈希表,就可以隨時判斷某一節(jié)點能否和之前路徑相加成為目標(biāo)值。 最新更新請見:https://yanjia.me/zh/2019/01/... Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addin...
摘要:和唯一的不同是組合中不能存在重復(fù)的元素,因此,在遞歸時將初始位即可。 Combination Sum I Problem Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T...
摘要:解題思路利用遞歸,對于每個根節(jié)點,只要左子樹和右子樹中有一個滿足,就返回每次訪問一個節(jié)點,就將該節(jié)點的作為新的進行下一層的判斷。代碼解題思路本題的不同點是可以不從開始,不到結(jié)束。代碼當(dāng)前節(jié)點開始當(dāng)前節(jié)點左節(jié)點開始當(dāng)前節(jié)點右節(jié)點開始 Path SumGiven a binary tree and a sum, determine if the tree has a root-to-lea...
摘要:和方法一樣,多一個數(shù),故多一層循環(huán)。完全一致,不再贅述, 4Sum Problem Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which ...
H-Index 題目鏈接:https://leetcode.com/problems... sort: public class Solution { public int hIndex(int[] citations) { if(citations == null || citations.length == 0) return 0; // sort ...
閱讀 2048·2019-08-30 15:52
閱讀 2440·2019-08-29 18:37
閱讀 790·2019-08-29 12:33
閱讀 2839·2019-08-29 11:04
閱讀 1522·2019-08-27 10:57
閱讀 2092·2019-08-26 13:38
閱讀 2759·2019-08-26 12:25
閱讀 2445·2019-08-26 12:23