【done】【重点】【回溯】剑指offer——面试题25:二叉树中和为某一值的路径

力扣

思路1:只有DFS,效率慢

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> pathTarget(TreeNode root, int target) {
        List<List<Integer>> res = new ArrayList<>();
        if (Objects.isNull(root)) {
            return res;
        }
        List<Integer> tmp = new ArrayList<>();
        find(root, target, res, tmp);
        return res;
    }

    public void find(TreeNode root, int residual, List<List<Integer>> res, List<Integer> tmp) {
        tmp.add(root.val);
        if (Objects.isNull(root.left) && Objects.isNull(root.right) && root.val == residual) {
            res.add(new ArrayList<>(tmp));
            return;
        }

        int curSize = tmp.size();
        if (!Objects.isNull(root.left)) {
            find(root.left, residual - root.val, res, tmp);
        }
        tmp = tmp.subList(0, curSize);
        if (!Objects.isNull(root.right)) {
            find(root.right, residual - root.val, res, tmp);
        }
    }
}

思路2:DFS + 回溯,速度快

// 简练写法
class Solution {
    public List<List<Integer>> pathTarget(TreeNode root, int target) {
        List<List<Integer>> res = new ArrayList<>();
        LinkedList<Integer> tmp = new LinkedList<>();
        find(root, target, res, tmp);
        return res;
    }

    public void find(TreeNode root, int residual, List<List<Integer>> res, LinkedList<Integer> tmp) {
        if (root == null) {
            return;
        }
        tmp.add(root.val);
        if (Objects.isNull(root.left) && Objects.isNull(root.right) && root.val == residual) {
            res.add(new ArrayList<>(tmp));
            tmp.removeLast(); // 回溯关键操作
            return;
        }
        find(root.left, residual - root.val, res, tmp);
        find(root.right, residual - root.val, res, tmp);
        tmp.removeLast();     // 回溯关键操作
    }
}

// 稍繁琐写法
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> pathTarget(TreeNode root, int target) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        List<Integer> tmp = new ArrayList<>();
        dfs(root, target, res, tmp);

        return res;
    }

    public void dfs(TreeNode root, int residual, List<List<Integer>> res, List<Integer> tmp) {
        if (residual == root.val && root.left == null && root.right == null) {
            tmp.add(root.val);
            res.add(new ArrayList<>(tmp));
            tmp.remove(tmp.size() - 1);
            return;
        }
        tmp.add(root.val);
        if (root.left != null) {
            dfs(root.left, residual - root.val, res, tmp);
        }
        if (root.right != null) {
            dfs(root.right, residual - root.val, res, tmp);
        }
        tmp.remove(tmp.size() - 1);
    }
}

参考网址:https://www.nowcoder.com/profile/5488508/codeBookDetail?submissionId=14432259
##Solution1:
Solution2写法更标准一些!

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<vector<int> > buffer;
    vector<int> tmp;
    vector<vector<int> > FindPath(TreeNode* root, int expectNumber) {
        if (root == NULL)
            return buffer;
        tmp.push_back(root->val);
        //如果是叶子结点,并且路径上结点和等于输入值
        //打印这条路径
        if ((expectNumber - root->val) == 0 && 
            root->left==NULL && root->right==NULL) {
            buffer.push_back(tmp);
        }
        //如果不是叶节点,则遍历它的子节点
        FindPath(root->left, expectNumber - root->val);
        FindPath(root->right, expectNumber - root->val);
        
        //在返回父结点之前,在路径上删除当前结点
        if(tmp.size() != 0)
            tmp.pop_back();
        return buffer;
    }
};

##Solution2:
教科书一样的DFS + Backtracking算法
注意和Solution3的细微差别,Solution3中没有backtracking的体现

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int>> ret;
        vector<int> trace;
        if(root)
            dfs(root,expectNumber,ret,trace);
        return ret;
    }
    void dfs(TreeNode* root,int s,vector<vector<int>> &ret,vector<int> &trace) {
        trace.push_back(root->val);
        if(!root->left&&!root->right) {
            if(s==root->val)
                ret.push_back(trace);
        }
        if(root->left)
            dfs(root->left,s-root->val,ret,trace);
        if(root->right)
            dfs(root->right,s-root->val,ret,trace);
        trace.pop_back();
    }
};

##Solution3:
20180901重做
DFS经典套路题目

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
bool cmp(vector<int> &a, vector<int> &b) {
    return a.size() > b.size() ? true : false; 
}
class Solution { 
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        if (!root) return {};
        vector<vector<int> > res;
        vector<int> temp;
        int residual = expectNumber;
        my_DFS(root, residual, res, temp);
        sort(res.begin(), res.end(), cmp);
        return res;
    }
    void my_DFS(TreeNode *root, int residual, 
                vector<vector<int> > &res, vector<int> temp) {
        if (!root->left && !root->right) { //到达叶子结点
            if (residual == root->val) {
                temp.push_back(root->val);
                res.push_back(temp);
            }
            return;
        } else { //一般情况
            temp.push_back(root->val);
            if (root->left) 
                my_DFS(root->left, residual - root->val, res, temp);
            if (root->right) 
                my_DFS(root->right, residual - root->val, res, temp);
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值