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));
    }
}
上一篇:两个月速成电磁场,957专业课113


下一篇:JS网页特效:星空飞入效果