欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

Java在線學(xué)習(xí)課程:LeetCode -- Path Sum III分析及實現(xiàn)方法

2018-05-02 10:59:16 1360瀏覽

廢話不多說了,下面和大家分享一下扣丁學(xué)堂Java在線學(xué)習(xí)課程:LeetCode -- Path Sum III分析及實現(xiàn)方法,希望可以幫到對Java開發(fā)感興趣的小伙伴們。



Java在線學(xué)習(xí)課程:LeetCode -- Path Sum III分析及實現(xiàn)方法




LeetCode -- Path Sum III分析及實現(xiàn)方法


題目描述:

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è)當(dāng)前節(jié)點為root,分別收集左右節(jié)點路徑和的集合,merge到當(dāng)前集合中;

將當(dāng)前節(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); 
 } 
}



好了,以上就是小編給大家分享的Java在線學(xué)習(xí)課程:LeetCode -- Path Sum III分析及實現(xiàn)方法,想要學(xué)好Java開發(fā)的小伙伴一定要選擇專業(yè)的Java培訓(xùn)機(jī)構(gòu),小編給大家推薦專業(yè)的Java培訓(xùn)機(jī)構(gòu)扣丁學(xué)堂,扣丁學(xué)堂不僅有專業(yè)的老師和與時俱進(jìn)的課程體系還有大量的Java在線教程供學(xué)員觀看學(xué)習(xí),心動的小伙伴快快行動吧。Java技術(shù)交流群:670348138。


關(guān)注微信公眾號獲取更多學(xué)習(xí)資料

關(guān)注微信公眾號獲取更多學(xué)習(xí)資料



查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>


標(biāo)簽: Java在線學(xué)習(xí)課程:LeetCode -- Path Sum III分析及實現(xiàn)方法 Java培訓(xùn) Java基礎(chǔ)教程 Java學(xué)習(xí)視頻 Java教學(xué)視頻 java入門教程 Java教程視頻 java在線學(xué)習(xí) java在線視頻 java在線教程

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部