zoukankan      html  css  js  c++  java
  • LeetCode: Path Sum I && II

    Title:

    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.

    思路:使用递归,如果是叶子节点进行判断。另外并不是每次都是&&啊,开始怎么想的。。。另外注释掉的语句并没有用,因为没有说节点值一定为正

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        bool hasPathSum(TreeNode* root, int sum) {
            if (!root)
                return false;
            if (!root->left && !root->right)
                return sum == root->val;
            //if (sum < 0)
                //return false;
            return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val);
        }
    };

    Title:

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

    For example:
    Given the below binary tree and sum = 22,

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

    return

    [
       [5,4,11,2],
       [5,8,4,5]
    ]
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
            vector<vector<int> > results;
            vector<int> result;
            pathSum(root,sum,results,result);
            return results;
            
        }
        void pathSum(TreeNode* root,int sum, vector<vector<int> > &results, vector<int> &result){
            if (!root)
                return ;
            if (!root->left && !root->right){
                if (root->val == sum){
                    result.push_back(root->val);
                    results.push_back(result);
                    result.pop_back();
                }
                return ;
            }
            result.push_back(root->val);
            pathSum(root->left,sum-root->val,results,result);
            pathSum(root->right,sum-root->val,results,result);
            result.pop_back();
        }
    };
  • 相关阅读:
    Web开发者不容错过的20段CSS代码
    常用统计学公式
    手工设置paraview 的几个问题
    响应qt消息应该注意的一点
    找不到QtDir变量的解决办法, 同时不需要经过编译就可以使用qt 库
    paraview的几个问题
    使用paraview 方法
    有志于webgl, three.js编程的朋友进入群52391108,72579998,共同学习提高,本人提供学习相关的资料,webgl,three.js学习,交流的论坛www.webglchina.cn
    qt模型视图结构
    opengl 场景转换过程
  • 原文地址:https://www.cnblogs.com/yxzfscg/p/4504393.html
Copyright © 2011-2022 走看看