---
title: "Find all paths with sum as a given value in a binary three"
author: "standalone"
date: "2012-09-01T11:40:37Z"
tags: []
categories: ["编程语言"]
slug: "find-all-paths-with-sum-as-a-given-value-in-a-binary-three"
draft: false
lastmod: "2012-09-01T11:41:39Z"
id: 1669300
url: "https://standalone.iteye.com/blog/1669300"
---

Problem:
microsoft-interview-questions 57 Answers

Given a value and a binary search tree.
Print all the paths(if there exists more than one) which sum up to that value. It can be any path in the tree. It doesn't have to be from the root.

My code:
I also pasted my code at <http://www.careercup.com/question?id=2971>


```java

import java.util.Stack;

public class BinarySearchTree {

	private class Node {
		int data;
		Node left;
		Node right;
		Node(int v, Node left, Node right){
			data = v;
			this.left = left;
			this.right = right;
		}		
	}

	public Node root;

	public void insertNode(int data) {
		if (root == null) {
			root = new Node(data, null, null);			
			return;
		}
		Node cur = root;
		do {
			if(data >= cur.data) {
				if(cur.right != null) cur = cur.right;
				else {
					cur.right = new Node(data, null, null);
					return;
				}
			}else {
				if(cur.left != null) cur = cur.left;
				else {
					cur.left = new Node(data, null, null);
					return;
				}
			}
		}while(cur != null);
	}

	public void inorder() {
		System.out.println("\nin-order :");
		printSubTreeInOrder(root);
	}
	
	public void preorder() {
		System.out.println("\npre-order :");
		printSubTreePreOrder(root);
	}
	
	private void printSubTreeInOrder(Node p){
		if(p.left != null) printSubTreeInOrder(p.left);
		System.out.print("\t" + p.data);
		if(p.right != null) printSubTreeInOrder(p.right);
	}
	private void printSubTreePreOrder(Node p){
		System.out.print("\t" + p.data);
		if(p.left != null) printSubTreePreOrder(p.left);		
		if(p.right != null) printSubTreePreOrder(p.right);
	}
	public void printAllPathWithSum(int sum){
		Stack<Node> path = new Stack<Node>();
		findPath(root, sum, path);
	}
	
	private void printPath(Stack<Node> path){
		System.out.print("\nFind Path:");
		for(Node n: path){
			System.out.print("\t" + n.data);
		}
	}
	
	private void findPath(Node p, int value, Stack<Node> path){
		if(p == null) return;
		if(p.data == value){
			path.push(p);
			printPath(path);
			path.pop();
		}else {
			path.push(p);
			if(p.left != null) findPath(p.left, value - p.data, path);
			if(p.right != null)findPath(p.right, value- p.data, path);
			path.pop();			
		}
		if(p.left != null) findPath(p.left, value , path);
		if(p.right != null)findPath(p.right, value, path);
	}

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		BinarySearchTree tree = new BinarySearchTree();
		tree.insertNode(5);
		tree.insertNode(2);
		tree.insertNode(7);
		tree.insertNode(1);
		tree.insertNode(3);
		tree.insertNode(9);
		tree.inorder();
		tree.preorder();
		tree.printAllPathWithSum(1);
		tree.printAllPathWithSum(0);
		tree.printAllPathWithSum(16);
		tree.printAllPathWithSum(9);
	}

}


```