回溯法 解决二叉树中找一条和为固定数值的路径(java)

问题描述

其中路径指的是从跟节点到叶子节点经过的路径

代码

package com.yuzhiyun;

import java.util.ArrayList;
import java.util.List;

public class FindPathInBinaryTree {

    /**
     * 二叉树的树结点
     */
    public static class BinaryTreeNode {
        int value;
        BinaryTreeNode left;
        BinaryTreeNode right;
    }

    /**
     * 输入一棵二叉树和一个整数, 打印出二叉树中结点值的和为输入整数的所有路径。 从树的根结点开始往下一直到叶销点所经过的结点形成一条路径。
     *
     * @param root
     *            树的根结点
     * @param expectedSum
     *            要求的路径和
     */
    public static void findPath(BinaryTreeNode root, int expectedSum) {
        // 创建一个链表,用于存放根结点到当前处理结点的所经过的结点
        List<Integer> list = new ArrayList<>();
        // 如果根结点不为空,就调用辅助处理方法
        if (root != null) {
            findPath(root, 0, expectedSum, list);
        }
    }

    /**
     * @param root
     *            当前要处理的结点
     * @param curSum
     *            当前记录的和(还未加上当前结点的值)
     * @param expectedSum
     *            要求的路径和
     * @param result
     *            根结点到当前处理结点的所经过的结点,(还未包括当前结点)
     */
    public static void findPath(BinaryTreeNode root, int curSum, int expectedSum, List<Integer> result) {
        // 如果结点不为空就进行处理
        if (root != null) {
            // 加上当前结点的值
            curSum += root.value;
            // 将当前结点入队
            result.add(root.value);
            // 如果当前结点的值小于期望的和
            if (curSum < expectedSum) {
                // 递归处理左子树
                findPath(root.left, curSum, expectedSum, result);
                // 递归处理右子树
                findPath(root.right, curSum, expectedSum, result);
            }
            // 如果当前和与期望的和相等
            else if (curSum == expectedSum) {
                // 当前结点是叶结点,则输出结果
                if (root.left == null && root.right == null) {
                    System.out.println(result);
                }
            }
            // 移除当前结点,向上回溯
            result.remove(result.size() - 1);
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //      10
        //     / \
        //    5 12
        //   /\
        //  4  7
        BinaryTreeNode root = new BinaryTreeNode();
        root.value = 10;
        root.left = new BinaryTreeNode();
        root.left.value = 5;
        root.left.left = new BinaryTreeNode();
        root.left.left.value = 4;
        root.left.right = new BinaryTreeNode();
        root.left.right.value = 7;
        root.right = new BinaryTreeNode();
        root.right.value = 12;
        // 有两条路径上的结点和为22
        System.out.println("findPath(root, 22);");
        findPath(root, 22);
        // 没有路径上的结点和为15
        System.out.println("findPath(root, 15);");
        findPath(root, 15);
        // 有一条路径上的结点和为19
        System.out.println("findPath(root, 19);");
        findPath(root, 19);
    }

}

运行结果

findPath(root, 22);
[10, 5, 7]
[10, 12]
findPath(root, 15);
findPath(root, 19);
[10, 5, 4]
以下是使用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、付费专栏及课程。

余额充值