pathSum - leetcode

pathSum 1

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.

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);

// comment:
// subtract/deduct each node.val from the target sum,
// UNTIL: if (root.left == null && root.right == null && sum - root.val == 0) return true;
//OTHERWISE: keep recursion and go deeper: return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);



        // First tentative idea: 
        // first sum all path, and store in an array  
        // then check their sum
        
    }
}

pathSum 2

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.
List<List<Integer>> resultList
這道二叉樹路徑之和在之前的基礎(chǔ)上又需要找出路徑 (可以參見我之前的博客 http://www.cnblogs.com/grandyang/p/4036961.html)慧耍,但是基本思想都一樣未辆,還是需要用深度優(yōu)先搜索DFS,只不過數(shù)據(jù)結(jié)構(gòu)相對復(fù)雜一點断箫,需要用到二維的vector瓜浸,而且每當(dāng)DFS搜索到新節(jié)點時澳淑,都要保存該節(jié)點。而且每當(dāng)找出一條路徑之后插佛,都將這個保存為一維vector的路徑保存到最終結(jié)果二位vector中杠巡。List<List<Integer>> resultList
并且,每當(dāng)DFS搜索到子節(jié)點雇寇,發(fā)現(xiàn)不是路徑和時氢拥,返回上一個結(jié)點時,需要把該節(jié)點從一維vector中移除锨侯。(轉(zhuǎn)載出處)

class Solution {
    private List<List<Integer>> resultList = new ArrayList<List<Integer>>();
    // A list of List<Integer>
    // List<List<Integer>> resultList
    public void pathSumInner(TreeNode root, int sum, Stack<Integer>path){
        
        path.push(root.val);
        
        if(root.left == null && root.right == null) {
            if (sum == root.val) resultList.add(new ArrayList<Integer>(path));
            // 只有最終符合條件的才會加入resultList
        }
        if(root.left != null) {
            pathSumInner(root.left, sum - root.val, path);
        }
        if(root.right != null) {
            pathSumInner(root.right, sum - root.val, path);
        }
        path.pop(); // 不符合條件的node嫩海,pop出Stack<Integer>(path)
    }
    
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        if (root == null) {return resultList;}
        Stack<Integer> path = new Stack<Integer>();
        pathSumInner(root, sum, path);
        return resultList;
    }
    
}

pathSum 3

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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市识腿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌造壮,老刑警劉巖渡讼,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異耳璧,居然都是意外死亡成箫,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門旨枯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蹬昌,“玉大人,你說我怎么就攤上這事攀隔≡矸罚” “怎么了栖榨?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長明刷。 經(jīng)常有香客問我婴栽,道長,這世上最難降的妖魔是什么辈末? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任愚争,我火速辦了婚禮,結(jié)果婚禮上挤聘,老公的妹妹穿的比我還像新娘轰枝。我一直安慰自己,他們只是感情好组去,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布鞍陨。 她就那樣靜靜地躺著,像睡著了一般添怔。 火紅的嫁衣襯著肌膚如雪湾戳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天广料,我揣著相機與錄音砾脑,去河邊找鬼。 笑死艾杏,一個胖子當(dāng)著我的面吹牛韧衣,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播购桑,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼畅铭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了勃蜘?” 一聲冷哼從身側(cè)響起硕噩,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎缭贡,沒想到半個月后炉擅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡阳惹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年谍失,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片莹汤。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡快鱼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情抹竹,我是刑警寧澤线罕,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站柒莉,受9級特大地震影響闻坚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜兢孝,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一窿凤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧跨蟹,春花似錦雳殊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至痢艺,卻和暖如春仓洼,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背堤舒。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工色建, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人舌缤。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓箕戳,卻偏偏與公主長得像,于是被迫代替她去往敵國和親国撵。 傳聞我的和親對象是個殘疾皇子陵吸,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容