C/C++教程

leetcode 113 路径总和II

本文主要是介绍leetcode 113 路径总和II,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

简介

路径总和 思路 回溯.
不推荐层次遍历, 代码比较复杂.

code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> res;
    int targetSum_;
    void dfs(vector<int> &path, int value, map<TreeNode*, bool>& visited, TreeNode * root){
        
        if(root->left == nullptr && root->right == nullptr && value == targetSum_){
            res.push_back(path);
        }

        if(root->left != nullptr && visited[root->left] == false) {
            path.push_back(root->left->val);
            value += root->left->val;
            visited[root->left] = true;
            dfs(path, value, visited, root->left);
            visited[root->left] = false;
            value -= root->left->val;
            path.pop_back();
        }
        if(root->right != nullptr && visited[root->right] == false){
            path.push_back(root->right->val);
            value += root->right->val;
            visited[root->right] = true;
            dfs(path, value, visited, root->right);
            visited[root->right] = false;
            value -= root->right->val;
            path.pop_back();
        }
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        targetSum_ = targetSum;
        if(root == nullptr) return res;
        vector<int> path;
        map<TreeNode *, bool> visited;
        visited[root] = true;
        path.push_back(root->val);
        dfs(path, root->val, visited, root);
        return res;

    }
};
class Solution {
    List<List<Integer>> ret = new LinkedList<List<Integer>>();
    Map<TreeNode, TreeNode> map = new HashMap<TreeNode, TreeNode>();

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        if (root == null) {
            return ret;
        }

        Queue<TreeNode> queueNode = new LinkedList<TreeNode>();
        Queue<Integer> queueSum = new LinkedList<Integer>();
        queueNode.offer(root);
        queueSum.offer(0);

        while (!queueNode.isEmpty()) {
            TreeNode node = queueNode.poll();
            int rec = queueSum.poll() + node.val;

            if (node.left == null && node.right == null) {
                if (rec == sum) {
                    getPath(node);
                }
            } else {
                if (node.left != null) {
                    map.put(node.left, node);
                    queueNode.offer(node.left);
                    queueSum.offer(rec);
                }
                if (node.right != null) {
                    map.put(node.right, node);
                    queueNode.offer(node.right);
                    queueSum.offer(rec);
                }
            }
        }

        return ret;
    }

    public void getPath(TreeNode node) {
        List<Integer> temp = new LinkedList<Integer>();
        while (node != null) {
            temp.add(node.val);
            node = map.get(node);
        }
        Collections.reverse(temp);
        ret.add(new LinkedList<Integer>(temp));
    }
}
这篇关于leetcode 113 路径总和II的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!