*【九度OJ1368】|【劍指offer25】二叉樹中和爲某一值的路徑

題目描述: java

輸入一顆二叉樹和一個整數,打印出二叉樹中結點值的和爲輸入整數的全部路徑。路徑定義爲從樹的根結點開始往下一直到葉結點所通過的結點造成一條路徑。 node

輸入:

每一個測試案例包括n+1行: 測試

第一行爲2個整數n,k(1<=n<=10000),n表示結點的個數,k表示要求的路徑和,結點編號從1到 n。                                                                                                        this

接下來有n行。這n行中每行爲3個整數vi,leftnode,rightnode,vi表示第i個結點的值,leftnode表示第i個結點的左孩子結點編號,rightnode表示第i個結點的右孩子結點編號,若無結點值爲-1。編號爲1的結點爲根結點。 spa

輸出:

對應每一個測試案例,先輸出「result:」佔一行,接下來按字典順序輸出知足條件的全部路徑,這些路徑由結點編號組成,輸出格式參照輸出樣例。 code

樣例輸入:
5 22
10 2 3
5 4 5
12 -1 -1
4 -1 -1
7 -1 -1
1 5
1 -1 -1
樣例輸出:
result:
A path is found: 1 2 5
A path is found: 1 3
result:
解:首先要想到的是經過遍歷能夠找到全部路徑,按照先序遍歷的思路,先將通過的左節點入棧,若是是進的是葉子節點,則判斷和是否符合要求,否和要求打印路徑將該結點出棧,不否和要求直接將結點出棧,而後繼續遍歷
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Stack;
 
public class Main {
    public static int target;
     
    static class Node{
        public int id;
        public int data;
        public Node left;
        public Node right;
        public Node(int id){
            this.id = id;
        }
    }
    public static Node find(int id, Node node){
        if(node != null){
            if(node.id == id)
                return node;
            Node l = find(id, node.left);
            Node r = find(id, node.right);
            if(l != null){
                return l;
            }else if(r != null){
                return r;
            }else
                return null;
        }else{
            return null;
        }
    }
    public static void search(Node node, Stack<Integer> stack, int sum){
        if(node == null || node.id == -1){
            return;
        }else{
            sum += node.data;
            stack.push(node.id);
            search(node.left, stack, sum);
            search(node.right, stack, sum);
            if(sum == target && node.left.id == -1 && node.right.id == -1){
                System.out.print("A path is found:");
                for(int i:stack){
                    System.out.print(" "+i);
                }
                System.out.println();
            }
            stack.pop();
        }
    }
     
    public static void main(String[] args) throws IOException {
         
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        while(st.nextToken() != StreamTokenizer.TT_EOF){
            int n = (int)st.nval;
            st.nextToken();
            Main.target = (int)st.nval;
            Node root = new Node(1);
            for(int i = 1; i <= n; i++){
                Node node = Main.find(i, root);
                st.nextToken();
                node.data = (int)st.nval;
                st.nextToken();
                int l = (int)st.nval;
                st.nextToken();
                int r = (int)st.nval;
                if(l > r){
                    node.left = new Node(r);
                    node.right = new Node(l);
                }else{
                    node.left = new Node(l);
                    node.right = new Node(r);
                }
            }
            System.out.println("result:");
            Main.search(root, new Stack<Integer>(), 0);
        }
    }
 
}



注:第一個案例沒經過 get

相關文章
相關標籤/搜索