二叉树的路径和

题目:二叉树的路径和


问题描述:给定一个二叉树,找出所有路径中各节点相加总和等于给定目标值 的路径。一个有效的路径,指的是从根节点到叶节点的路径。


思路:找到和为目标值的路径,输出路径上的元素。


代码:

class Solution {
public:
    vector<vector<int>>p;
    /**
     * @param root the root of binary tree
     * @param target an integer
     * @return all valid paths
     */
    vector<vector<int>> binaryTreePathSum(TreeNode *root, int target) {
        // Write your code here
        vector<int>v;
        int s=0;
        find(root,target,v,s);
        return p;
    }
    void find(TreeNode *root,int target,vector<int>v,int s)
    {
        if(root!=NULL)
        {
            v.push_back(root->val);
            s=s+root->val;
        if(s==target&&root->left==NULL&&root->right==NULL)
        {
            p.push_back(v);
        }
        if(root->left!=NULL)
        {
            find(root->left,target,v,s);
        }
        if(root->right!=NULL)
        {
            find(root->right,target,v,s);
        }
        s=s-root->val;
        v.pop_back();
        }
    }
};


感想:考虑问题要详细!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用Java实现迭代法求二叉树路径和的代码,其中包括求出所有满足目标和的数组,并返回数组值的功能: ```java import java.util.ArrayList; import java.util.List; import java.util.Stack; public class BinaryTreePathSum { private static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public static List<List<Integer>> pathSum(TreeNode root, int sum) { List<List<Integer>> res = new ArrayList<>(); if (root == null) { return res; } Stack<TreeNode> stack = new Stack<>(); stack.push(root); Stack<Integer> sumStack = new Stack<>(); sumStack.push(sum - root.val); Stack<List<Integer>> pathStack = new Stack<>(); List<Integer> path = new ArrayList<>(); path.add(root.val); pathStack.push(path); while (!stack.isEmpty()) { TreeNode node = stack.pop(); int curSum = sumStack.pop(); List<Integer> curPath = pathStack.pop(); if (node.left == null && node.right == null && curSum == 0) { res.add(curPath); } if (node.right != null) { stack.push(node.right); sumStack.push(curSum - node.right.val); List<Integer> rightPath = new ArrayList<>(curPath); rightPath.add(node.right.val); pathStack.push(rightPath); } if (node.left != null) { stack.push(node.left); sumStack.push(curSum - node.left.val); List<Integer> leftPath = new ArrayList<>(curPath); leftPath.add(node.left.val); pathStack.push(leftPath); } } return res; } public static void main(String[] args) { TreeNode root = new TreeNode(5); root.left = new TreeNode(4); root.right = new TreeNode(8); root.left.left = new TreeNode(11); root.left.left.left = new TreeNode(7); root.left.left.right = new TreeNode(2); root.right.left = new TreeNode(13); root.right.right = new TreeNode(4); root.right.right.left = new TreeNode(5); root.right.right.right = new TreeNode(1); int sum = 22; List<List<Integer>> res = pathSum(root, sum); for (List<Integer> path : res) { System.out.println(path); } } } ``` 在这个例子中,二叉树如下所示: ``` 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 ``` 目标和为22,那么结果应该是[[5, 4, 11, 2], [5, 8, 4, 5]]。运行上述代码可以得到正确的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值