国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

LeetCode -- Path Sum III

2019-11-08 02:11:09
字體:
供稿:網(wǎng)友
題目描述: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.給定一個二叉樹,遍歷過程中收集所有可能路徑的和,找出和等于X的路徑樹。思路:設(shè)當前節(jié)點為root,分別收集左右節(jié)點路徑和的集合,merge到當前集合中;將當前節(jié)點添加到數(shù)組中,構(gòu)成新的可能路徑。實現(xiàn)代碼:
/** * Definition for a binary tree node. * public class TreeNode { *     public int val; *     public TreeNode left; *     public TreeNode right; *     public TreeNode(int x) { val = x; } * } */public class Solution {        PRivate int _sum;    private int _count;    public int PathSum(TreeNode root, int sum)     {		_count = 0;		_sum = sum;    	Travel(root, new List<int>());    	return _count;    }        private void Travel(TreeNode current, List<int> ret){		if(current == null){			return ;		}				if(current.val == _sum){			_count ++;		}				var left = new List<int>();		Travel(current.left, left);				var right = new List<int>();		Travel(current.right, right);				ret.AddRange(left);		ret.AddRange(right);				for(var i = 0;i < ret.Count; i++){			ret[i] += current.val;			if(ret[i] == _sum){				_count ++;			}		}		ret.Add(current.val);				//Console.WriteLine(ret);    }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 镇雄县| 庐江县| 贵港市| 渝北区| 双城市| 五大连池市| 竹北市| 阳原县| 仁怀市| 吉林省| 沾益县| 江山市| 集安市| 黄骅市| 嘉鱼县| 顺昌县| 云梦县| 任丘市| 黑水县| 惠安县| 德化县| 增城市| 永吉县| 广德县| 衡东县| 西宁市| 苍南县| 阳朔县| 白朗县| 承德县| 台北县| 巩留县| 黑龙江省| 襄汾县| 任丘市| 边坝县| 京山县| 应用必备| 凤翔县| 抚远县| 平远县|