[LeetCode] 112. 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.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

      5
     / \
    4   8
   /   / \
  11  13  4
 /  \      \
7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

 

這道題給了一棵二叉樹,問是否存在一條從跟結點到葉結點到路徑,使得經過到結點值之和為一個給定的 sum 值,這里需要用深度優先算法 DFS 的思想來遍歷每一條完整的路徑,也就是利用遞歸不停找子結點的左右子結點,而調用遞歸函數的參數只有當前結點和 sum 值。首先,如果輸入的是一個空結點,則直接返回 false,如果如果輸入的只有一個根結點,則比較當前根結點的值和參數 sum 值是否相同,若相同,返回 true,否則 false。 這個條件也是遞歸的終止條件。下面就要開始遞歸了,由於函數的返回值是 Ture/False,可以同時兩個方向一起遞歸,中間用或 || 連接,只要有一個是 True,整個結果就是 True。遞歸左右結點時,這時候的 sum 值應該是原 sum 值減去當前結點的值,參見代碼如下:

 

解法一:

class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if (!root) return false;
        if (!root->left && !root->right && root->val == sum ) return true;
        return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
    }
};

 

我們也可以使用迭代的寫法,這里用的也是先序遍歷的迭代寫法,先序遍歷二叉樹,左右子結點都需要加上其父結點值,這樣當遍歷到葉結點時,如果和 sum 相等了,那么就說明一定有一條從 root 過來的路徑。注意這里不必一定要先處理右子結點,調換下順序也是可以的,因為不論是先序遍歷的根-左-右,還是根-右-左,並不會影響到找路徑,參見代碼如下:

 

解法二:

class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if (!root) return false;
        stack<TreeNode*> st{{root}};
        while (!st.empty()) {
            TreeNode *t = st.top(); st.pop();
            if (!t->left && !t->right) {
                if (t->val == sum) return true;
            }
            if (t->right) {
                t->right->val += t->val;
                st.push(t->right);
            }
            if (t->left) {
                t->left->val += t->val;
                st.push(t->left);
            }
        }
        return false;
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/112

 

類似題目:

Path Sum IV 

Path Sum III  

Binary Tree Maximum Path Sum

Path Sum II

Sum Root to Leaf Numbers

Binary Tree Preorder Traversal

 

參考資料:

https://leetcode.com/problems/path-sum/

https://leetcode.com/problems/path-sum/discuss/36534/My-java-no-recursive-method

https://leetcode.com/problems/path-sum/discuss/36378/AcceptedMy-recursive-solution-in-Java

https://leetcode.com/problems/path-sum/discuss/36382/Accepted-By-using-postorder-traversal

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM