叶子的路径在二叉树中有特定的总和

时间:2012-03-09 23:56:04

标签: java binary-tree

我知道如何查找二叉树是否具有给定总和的特定路径(如果这不是最好的方式请告诉我):

int pathSum(MyNode root, int sum)
{
    if(root == null)
        return -1;
    int temp = sum - root.value;
    return(pathSum(root.left,temp) || pathSum(root.right,temp));
}

我无法弄清楚如何打印特定路径。

我的Node类看起来像这样:

class MyNode {
    int value;
    MyNode left;
    MyNode right;

    MyNode(int value)
    {
        this.value = value;
    }
}

4 个答案:

答案 0 :(得分:4)

试试这个,使用重载:

 public void pathToSum(int sum) {
    pathToSum(root, sum);
}

private boolean pathToSum(Node n, int sum) {
    if (null != n) {
        sum -= n.data;
        boolean found = pathToSum(n.left, sum);

        if (!found) {
            found = pathtoSum(n.right, sum);
        }
        if (found) {
            println(n.data);
                            return found;
        }
    }
    return 0 == sum ? true : false;
}

此代码使用以下类进行测试:

import java.util.LinkedList;
import java.util.Queue;
public class BST {
Node root;
public BST(){
    root = null;
}

public void insert(int el){

    Node tmp = root, p=null;
    while(null!=tmp && el != tmp.data){
        p=tmp;
        if(el<tmp.data)
            tmp=tmp.left;
        else
            tmp=tmp.right;
    }
    if(tmp == null){
        if(null == p)
            root = new Node(el);
        else if(el <p.data)
            p.left= new Node(el);
        else
            p.right=new Node(el);
    }
}//

public void pathToSum(int sum) {
    pathToSum(root, sum);
}//

private boolean pathToSum(Node n, int sum) {
    if (null != n) {
        sum -= n.data;
        boolean found = pathToSum(n.left, sum);

        if (!found) {
            found = pathToSum(n.right, sum);
        }
        if (found) {
            System.out.println(n.data);
            return found;
        }
    }
    return 0 == sum ? true : false;
}

public static void main(String[] args){
    int[] input={50,25,75,10,35,60,100,5,20,30,45,55,70,90,102};
    BST bst = new BST();
    for(int i:input)
        bst.insert(i);
    bst.pathToSum(155);
}
}

class Node{
public int data;
public Node left;
public Node right;
public Node(int el){
    data = el;
}
}

结果:

45
35
25
50

答案 1 :(得分:1)

我建议更改MyNode类以包含父节点:

MyNode left;
MyNode right;
MyNode parent;

MyNode(int value, MyNode parent)
{
    this.value = value;
    this.parent = parent;
}

然后当您使用正确的总和命中节点时,您可以将该节点传递给另一个通过祖先的函数,直到它到达具有空父(根)的节点。

答案 2 :(得分:1)

好的谜题,我喜欢它。你几乎拥有它,只是对int与boolean的混淆,并没有检查sum的结束条件为零。

public class NodeSums {

    static boolean pathSum(MyNode root, int sum) {
        boolean ret;
        if (root == null) {
            ret = sum == 0;
        } else {
            int remain = sum - root.value;
            ret = pathSum(root.left,remain) || pathSum(root.right, remain);
        }
        return ret;
    }

    static class MyNode {
        int value;
        MyNode left;
        MyNode right;

        MyNode(int value) {
            this.value = value;
        }
    }

    public static void main(String[] args) {

        /**
         * Valid sums will be 3, 8, and 9
         * 
         *    1 -- 2 
         *      --
         *      -- 3 -- 4
         *           --
         *           -- 5
         */
        MyNode root = new MyNode(1);
        root.left = new MyNode(2);
        root.right = new MyNode(3);
        root.right.left = new MyNode(4);
        root.right.right = new MyNode(5);

        for (int i = 1; i < 10; i++) {
            System.out.println("Path sum " + i + " " + pathSum(root, i));
        }
    }
}

输出

Path sum 1 false
Path sum 2 false
Path sum 3 true
Path sum 4 false
Path sum 5 false
Path sum 6 false
Path sum 7 false
Path sum 8 true
Path sum 9 true

答案 3 :(得分:0)

如果将每个节点的父节点存储在MyNode中,则可以通过在循环中获取父节点来找到从根节点到任何节点的(反向)路径,直到它为空。

此外,您的pathSum代码似乎是混合布尔值和整数,而您从不检查总和的值。