題目:在二叉樹中找出和爲某一值的所有路徑(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;
}
}
結果截圖: