zoukankan      html  css  js  c++  java
  • Leetcode Path Sum

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
     /**
     * https://oj.leetcode.com/problems/path-sum/
     * 參考http://www.cnblogs.com/remlostime/archive/2012/11/13/2767746.html
     */
     
    
     
    class Solution {
    public:
        bool hasPathSum(TreeNode *root, int sum) {
            return hasSum(root, sum, 0);
        }
        
        bool hasSum(TreeNode *node, int sum, int curSum){
            
            if(node == NULL){
                return false;
            }
            
            if(!node->left && !node->right){
                return (sum == curSum + node->val);
            }
            
            return hasSum(node->left, sum, curSum + node->val) || hasSum(node->right, sum, curSum + node->val);
        }
    };

  • 相关阅读:
    Unique path
    *Jump Game
    Valid Palindrome
    *Reverse Words in a String
    Min Stack
    [?]*Simplify Path
    *Valid Parentheses
    *Sqrt(x)
    String to Integer (atoi)
    Add Digits
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5181842.html
Copyright © 2011-2022 走看看