【练习】在二叉树中找出和为某个值的一条路径

二叉树中节点中只包含正整数值。
使用递归的方式实现,用一个双端队列保存已经经过的节点。到达一个节点的时候,比较当前的和再加上当前节点值是否等于目标值,若等于则输出已经经过的节点和当前节点;若不等于,则将当前节点追加到队列的尾部,再前往当前节点的子节点。

import java.util.LinkedList;

public class FindSum {
            //树中的节点类
            static class TreeNode{
                int val;
                TreeNode left,right;

                public TreeNode(int val){
                    this.val=val;
                    left=null;
                    right=null;
                }
            }

    public static void find(LinkedList<TreeNode> list,TreeNode root,int curSum,int target){

        if(curSum+root.val == target){
            for(TreeNode node:list){
                System.out.print(node.val+" ");
            }
            System.out.println(root.val);
        }

        if(root.left != null){
                list.addLast(root);
                find(list,root.left,curSum+root.val,target);
                list.removeLast();
        }
        if(root.right !=null){
                list.addLast(root);
                find(list,root.right,curSum+root.val,target);
                list.removeLast();
        }

    }

    //中序遍历函数,不是必须的
    public static void display(TreeNode root){
        if(root.left != null)
            display(root.left);
        System.out.print(root.val+" ");
        if(root.right != null)
            display(root.right);
    }

    public static void main(String[] args){
        //手动构造一棵树       
        TreeNode n1=new TreeNode(5);
        TreeNode n2=new TreeNode(4);
        TreeNode n3=new TreeNode(9);
        TreeNode n4=new TreeNode(11);
        TreeNode n5=new TreeNode(2);
        TreeNode n6=new TreeNode(7);
        TreeNode n7=new TreeNode(3);
        TreeNode n8=new TreeNode(14);
        TreeNode n9=new TreeNode(4);

        n1.left=n2;
        n1.right=n3;
        n2.left=n4;
        n2.right=n5;
        n3.left=n6;
        n3.right=n7;
        n5.right=n8;
        n6.left=n9;

//      display(n1);

        LinkedList<TreeNode> list=new LinkedList<TreeNode>();
        find(list,n1,0,25);
        System.out.println("done");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言实现在二叉树找出和为某一的所有路径的示例代码: ```c #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; void findPath(TreeNode* root, int sum, int* path, int depth) { if (root == NULL) { return; } path[depth] = root->val; int i, curSum = 0; for (i = depth; i >= 0; --i) { curSum += path[i]; if (curSum == sum) { int j; for (j = i; j < depth; ++j) { printf("%d->", path[j]); } printf("%d\n", path[depth]); } } findPath(root->left, sum, path, depth + 1); findPath(root->right, sum, path, depth + 1); } int main() { // 构造二叉树 TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = 10; root->left = (TreeNode*)malloc(sizeof(TreeNode)); root->left->val = 5; root->right = (TreeNode*)malloc(sizeof(TreeNode)); root->right->val = 12; root->left->left = (TreeNode*)malloc(sizeof(TreeNode)); root->left->left->val = 4; root->left->right = (TreeNode*)malloc(sizeof(TreeNode)); root->left->right->val = 7; // 在二叉树查找和为22的路径 int path[100]; findPath(root, 22, path, 0); // 释放二叉树空间 free(root->left->left); free(root->left->right); free(root->left); free(root->right); free(root); return 0; } ``` 该示例代码,我们先构造了一棵二叉树,并在其查找和为22的路径。在查找路径的过程,我们使用了深度优先遍历的方法,通过一个数组来保存当前已经走过的路径,每次遍历到一个节点,则将该节点的加入路径。在加入之后,我们检查路径是否存在和为目标的子路径,如果存在,则输出该路径。最后,我们递归遍历该节点的左子树和右子树,以便查找所有可能的路径。 需要注意的是,该示例代码并未考虑二叉树存在负数的情况,如有需要,可以根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值