Leetcode112.Path Sum

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.


Approach1: DFS


This problem is quite similar to the LeetCode104, I use two stacks, one for node and one for node value. This is a pre-order idea to process nodes. Each time when I process the node, I process the value of it and update the sum of it. Cause the problem is asked about the root-leaf. I thought a valid path is right. that's the mistake I made at first. So I need to check if the node has child even if the value is equal to sum.
class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null) return false;
        Stack<TreeNode> stack = new Stack<>();
        Stack<Integer> value = new Stack<>();
        stack.push(root);
        value.push(root.val);
        while(!stack.empty()){
            TreeNode node = stack.pop();
            int temp = value.pop();
            if(temp == sum && node.left == null && node.right == null) {//follow up may exist here
                return true;
            }
            if(node.left != null){
                stack.push(node.left);
                value.push(node.left.val + temp);
            }
            if(node.right != null){
                stack.push(node.right);
                value.push(node.right.val + temp);
            }     
        }
        return false;
    }
}
Follow up question my be like: Find a valid path is fine, no need to root-leaf. Then just remove the condition "node.left == null && node.right == null".

Approach2 Recursion


The basic idea is to subtract the value of current node from sum until it reaches a leaf node and the subtraction equals 0, then we know that we got a hit. Otherwise the subtraction at the end could not be 0. Recursion pattern goes like update the parameter while call it self.
 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);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,862評(píng)論 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    網(wǎng)事_79a3閱讀 12,947評(píng)論 3 20
  • 舟兒 第一章 書(shū)房?jī)?nèi),女子靜靜立于畫(huà)屏旁處,看著掛在上面的畫(huà)軸,畫(huà)軸里一青一白的身影。女子一襲青白羅裙...
    柳歸舟閱讀 789評(píng)論 3 11
  • 日精進(jìn) 我每天的工作:每天要熟記客人信息,耐心和客人溝通 ,詳細(xì)并合理登記預(yù)約洗澡時(shí)間和需要用車(chē)時(shí)間。合理,明確,...
    呂志萍閱讀 147評(píng)論 0 3
  • 我自幼就喜愛(ài)搗鼓機(jī)械,小時(shí)候媽媽買(mǎi)來(lái)的玩具汽車(chē)、摩托車(chē),不到一個(gè)小時(shí)就在我的手中變成了零件,然后,大部分玩具就保持...
    雒渭閱讀 272評(píng)論 9 2

友情鏈接更多精彩內(nèi)容