Leetcode——Path Sum系列及unique paths系列

13 篇文章 0 订阅

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.

    bool hasPathSum(TreeNode* root, int sum) {
        if(root == NULL)
            return false;
        if(root -> left == NULL && root -> right == NULL)
            return root -> val == sum;
        return hasPathSum(root -> left, sum - root -> val) || hasPathSum(root -> right, sum - root -> val);
    }

 

113. Path Sum II

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

Note: A leaf is a node with no children.

    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int> > result;
        vector<int> cur;
        pathSum(root, sum, cur, result);
        return result;
    }
    void pathSum(TreeNode* root, int gap, vector<int>& path, vector<vector<int> >& result)
    {
        if(root == NULL)
            return;
        path.push_back(root -> val);
        if(root -> left == NULL && root -> right == NULL)
        {
            if(root -> val == gap)
                result.push_back(path);
            path.pop_back();
            return;
        }
        pathSum(root -> left, gap - root -> val, path, result);
        pathSum(root -> right, gap - root -> val, path, result);
        path.pop_back();
    }

 

437. Path Sum III

You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

法:利用了前序遍历,对于每个遍历到的节点进行处理,维护一个变量pre来记录之前路径之和,然后cur为pre加上当前节点值,如果cur等于sum,那么返回结果时要加1,然后对当前节点的左右子节点调用递归函数求解,最终结果为从根出发及从其左右子树分别出发路径数目的总和。

    int pathSum(TreeNode* root, int sum) {
        if (!root) 
            return 0;
        return sumUp(root, 0, sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
    }
    int sumUp(TreeNode* node, int pre, int& sum) {
        if (!node) 
            return 0;
        int cur = pre + node -> val;
        return (cur == sum) + sumUp(node->left, cur, sum) + sumUp(node->right, cur, sum);
    }

 

62. Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?


Above is a 7 x 3 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

法: dp[i][j] = dp[i- 1][j] + dp[i][j - 1];

    int uniquePaths(int m, int n) {
        if(m == 0 || n == 0)
            return 1;
        vector<vector<int>> dp(m, vector<int>(n));
        for(int i = 0; i < m; i ++)
            dp[i][0] = 1;
        for(int j = 0; j < n; j ++)
            dp[0][j] = 1;
        for(int i = 1; i < m; i ++)
            for(int j = 1; j < n; j ++)
                dp[i][j] = dp[i- 1][j] + dp[i][j - 1];
        return dp[m-1][n-1];
    }

 

63. Unique Paths II

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        vector<vector<int> > dp(m, vector<int>(n,0));
        for(int i = 0; i < m; i ++)
            if(obstacleGrid[i][0] == 1)
                break;
            else
                dp[i][0] = 1;
        for(int j = 0; j < n; j ++)
            if(obstacleGrid[0][j] == 1)
                break;
            else
                dp[0][j] = 1;
        for(int i = 1; i < m; i ++)
            for(int j = 1; j < n; j++)
                if(obstacleGrid[i][j] != 1)
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
        return dp[m - 1][n - 1];
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值