华为机试题之在二叉树中找出和为某一值的所有路径(java语言)

题目:在二叉树中找出和为某一值的所有路径(java语言)

描述:
请写一个程序创建一棵二叉树,并按照一定规则,输出二叉树根节点到叶子节点的路径。
规则如下:
1、从最顶端的根结点,到最下面的叶子节点,计算路径通过的所有节点的和,如果与设置的某一值的相同,那么输出这条路径上的所有节点。
2、从根节点遍历树时,请请按照左到右遍历,即优先访问左子树的节点。

二叉树创建规则:从上到下一层一层的,按照从左到右的顺序进行构造
输入”10,5,12,4,7”值,构造的树如下:
1) 10

2) 10
/
5

3) 10
/\
5 12

4) 10
/\
5 12
/
4

5) 10
/\
5 12
/\
4 7
针对上面的二叉树,如果当前我们设置的“路径和”为19,那么输出结果为:
10,5,4
如果有多个路径,按到左到右的顺序遍历生成的结果每行显示一个显示。例如如果当前我们设置的“路径和”为22,那么输出结果为:
10,5,7
10,12
如果没有找到路径和为设置的值的路径,输出error。

输入: 输入整数N—路径和一行字符串,多个正整数,之间用”,”隔开

输出: 满足条件的二叉树路径

样例输入:
22
10,5,12,4,7

样例输出:
10,5,7
10,12

代码:

package hw8_16.three.two.standard;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

public class Main {


    public static int index = 0;


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextLine()) {

            String total =scanner.nextLine();

            String string = scanner.nextLine();

            String[] arr = string.split(",");


            //1. 建立满二叉树
            TreeNode root = createFullBinTree(arr);

            //2. 判断
            List<Integer> pathList = new ArrayList<Integer>();

            int sum = 0;
            int[] flag = new int[]{0,0};
            judgePathSum(root,pathList,sum,Integer.parseInt(total),flag);

            if (flag[0] == 0) {
                System.out.println("error");
            }


        }


    }




    public static void judgePathSum(TreeNode root, List<Integer> pathList,int sum,int total,int[] flag) {
        // TODO Auto-generated method stub

        //先序遍历思想
        List<Integer> newPathList = new ArrayList<Integer>();

        newPathList.addAll(pathList);

        newPathList.add(root.getData());

        int newSum = sum+root.getData();

        //1.如何当前节点值总和大于total或者当前节点已经是叶子节点且节点值总和小于total则返回
        if (newSum > total ||
                (root.getLeftChild() == null && root.getLeftChild() == null && newSum < total)) {

            return;
        }else {

            //2.1 当前节点已经是叶子节点且节点值总和等于total则输出结果并返回
            if (root.getLeftChild() == null && root.getLeftChild() == null && newSum == total) {

                flag[0] = 1;
                for (int i = 0; i < newPathList.size()-1; i++) {
                    System.out.print(newPathList.get(i)+",");
                }
                System.out.println(newPathList.get(newPathList.size()-1));
                return;
            }

            //2.2 当前节点不是叶子节点,则继续
            if (root.getLeftChild() != null || root.getRightChild() != null) {

                //左
                if (root.getLeftChild() != null) {
                    judgePathSum(root.getLeftChild(),newPathList,newSum,total,flag);

                }

                //右
                if (root.getRightChild() != null) {
                    judgePathSum(root.getRightChild(),newPathList,newSum,total,flag);
                }


            }


        }


    }


    public static TreeNode  createFullBinTree(String[] arr){

        TreeNode root = new TreeNode(0, Integer.parseInt(arr[0]),false, null, null);

        Queue<TreeNode> queue = new LinkedList<TreeNode>();

        queue.add(root);

        int i = 1;

        while (!queue.isEmpty() && i < arr.length) {

            TreeNode temNode= queue.poll();

            //先建立左子数,并左子树进队列
            if (i < arr.length) {


                TreeNode treeNode = new TreeNode(0, Integer.parseInt(arr[i++]), false, null, null);

                //System.out.println("左"+treeNode.getData());

                temNode.setLeftChild(treeNode);

                //左子数进队列
                queue.add(treeNode);

            }

            //后建立右子数,并右子树进队列
            if (i < arr.length) {

                TreeNode treeNode = new TreeNode(0, Integer.parseInt(arr[i++]), false, null, null);

                temNode.setRightChild(treeNode);

                //System.out.println("右"+treeNode.getData());

                //左子数进队列
                queue.add(treeNode);

            }


        }



        return root;


    }


}


/**
 * 二叉树的节点数据结构
 * @author Administrator
 *
 */
class TreeNode{
    private int key = 0;
    private int data =0;

    private boolean isVisted = false;

    private TreeNode leftChild = null;
    private TreeNode rightChild = null;


    public TreeNode() {
        // TODO Auto-generated constructor stub
    }


    public TreeNode(int key, int data, boolean isVisted,
            TreeNode leftChild, TreeNode rightChild) {
        super();
        this.key = key;
        this.data = data;
        this.isVisted = isVisted;
        this.leftChild = leftChild;
        this.rightChild = rightChild;
    }


    public int getKey() {
        return key;
    }


    public void setKey(int key) {
        this.key = key;
    }


    public int getData() {
        return data;
    }


    public void setData(int data) {
        this.data = data;
    }





    public boolean isVisted() {
        return isVisted;
    }


    public void setVisted(boolean isVisted) {
        this.isVisted = isVisted;
    }


    public TreeNode getLeftChild() {
        return leftChild;
    }


    public void setLeftChild(TreeNode leftChild) {
        this.leftChild = leftChild;
    }


    public TreeNode getRightChild() {
        return rightChild;
    }


    public void setRightChild(TreeNode rightChild) {
        this.rightChild = rightChild;
    }



}








结果截图:
结果1
结果2

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值