• 【Leetcode】【Easy】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.

    For 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.

    递归的解法:

    只考虑当前结点的成败条件,对于儿子,则交给递归去做。

    读到某个结点,计算路径val之和,之后判断,如果不是叶子结点,递归调用函数进入其子孙结点。如果是叶子结点,当val之和与sum值相等,返回true,不相等返回false。

    注意:

    1、注意题目是“root-to-leaf”即计算根节点到叶子节点的加和,不要只计算到某个枝干,即使运算到某个枝干时,sum值已经相等,也不能返回true;

    2、注意可能出现正数负数混杂的情况;

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     bool hasPathSum(TreeNode *root, int sum) {
    13         if (!root)
    14             return false;
    15             
    16         int curSum = sum - root->val;
    17         
    18         if (!root->left && !root->right && curSum == 0)
    19             return true;
    20 
    21         return hasPathSum(root->left, curSum) || 
    22                 hasPathSum(root->right, curSum);
    23         
    24     }
    25 };

    迭代的解法:

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

    附录:

    用迭代法遍历二叉树思路总结

  • 相关阅读:
    SQL应用初级指南
    XML 文档的基本操作
    SQL中单引号的转义
    C# (输入输出流)
    C# 文件与目录的基本操作(System.IO)
    数据库对象命名
    .Net 中的反射(反射特性) Part.3 (转载)
    C# 中的委托和事件(详解)
    SQL Server TransactSQL 编程
    Brush 色谱
  • 原文地址:https://www.cnblogs.com/huxiao-tee/p/4114475.html
Copyright © 2020-2023  润新知