国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 新蔡县| 博白县| 中牟县| 桃园县| 五指山市| 凌源市| 舞钢市| 库车县| 赫章县| 基隆市| 开原市| 马山县| 绥阳县| 施甸县| 隆安县| 万山特区| 江永县| 罗田县| 临猗县| 九龙城区| 镇江市| 平利县| 福建省| 九龙县| 墨江| 富锦市| 瑞金市| 开封县| 凤城市| 长沙县| 寻乌县| 沙雅县| 淮阳县| 雅安市| 襄城县| 井陉县| 上饶县| 濮阳县| 韶关市| 韶关市| 大石桥市|