[LeetCode] Path Sum

简介: This problem has a very easy recursive code as follows. 1 class Solution { 2 public: 3 bool hasPathSum(TreeNode* root, int sum) { 4 ...

This problem has a very easy recursive code as follows.

 1 class Solution {
 2 public:
 3     bool hasPathSum(TreeNode* root, int sum) {
 4         if (!root) return false;
 5         if (!(root -> left) && !(root -> right))
 6             return sum == root -> val;
 7         return hasPathSum(root -> left, sum - root -> val) ||
 8                hasPathSum(root -> right, sum - root -> val);
 9     }
10 };

If you insist on an iterative solution, this link has a clean iterative solution in Python. I rewrite the code in C++ as follows. In fact, it uses pair<TreeNode*, int> to recor the information, which greatly simplifies the code.

 1 class Solution {
 2 public:
 3     bool hasPathSum(TreeNode* root, int sum) {
 4         if (!root) return false;
 5         stack<pair<TreeNode*, int> > pairs;
 6         pairs.push(make_pair(root, sum));
 7         while (!pairs.empty()) {
 8             pair<TreeNode*, int> pr = pairs.top();
 9             pairs.pop();
10             TreeNode* node = pr.first;
11             int remain = pr.second;
12             if (!(node -> left) && !(node -> right) && remain == node -> val)
13                 return true;
14             if (node -> left)
15                 pairs.push(make_pair(node -> left, remain - node -> val));
16             if (node -> right)
17                 pairs.push(make_pair(node -> right, remain - node -> val));
18         }
19         return false;
20     }
21 };

This link gives a longer iterative solution using postorder traversal.

 1 class Solution {
 2 public:
 3     bool hasPathSum(TreeNode* root, int sum) {
 4         TreeNode* pre = NULL;
 5         TreeNode* cur = root;
 6         stack<TreeNode*> nodes;
 7         int s = 0;
 8         while (cur || !nodes.empty()) {
 9             while (cur) {
10                 nodes.push(cur);
11                 s += cur -> val;
12                 cur = cur -> left;
13             }
14             cur = nodes.top();
15             if (!(cur -> left) && !(cur -> right) && s == sum)
16                 return true;
17             if (cur -> right && pre != cur -> right)
18                 cur = cur -> right;
19             else {
20                 pre = cur;
21                 nodes.pop();
22                 s -= cur -> val;
23                 cur = NULL;
24             }
25         }
26         return false;
27     }
28 };

 

目录
相关文章
LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid
LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
68 0
LeetCode 329. Longest Increasing Path in a Matrix
|
Go
LeetCode 124. Binary Tree Maximum Path Sum
给定一个非空二叉树,返回其最大路径和。 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。
67 0
LeetCode 124. Binary Tree Maximum Path Sum
|
Unix Python
LeetCode 71. Simplify Path
给定文件的绝对路径(Unix下的路径)字符串,简化此字符串。
84 0
LeetCode 71. Simplify Path
LeetCode 64. Minimum Path Sum
给定m x n网格填充非负数,找到从左上到右下的路径,这最小化了沿其路径的所有数字的总和。 注意:您只能在任何时间点向下或向右移动。
96 0
LeetCode 64. Minimum Path Sum
Leetcode-Easy 437. Path Sum III
Leetcode-Easy 437. Path Sum III
90 0
Leetcode-Easy 437. Path Sum III