简体   繁体   中英

Convert a tree in 2d array with depth first search

I have a binary tree the following binary tree, which needs to be converted into a 2d array. 二叉树

[[10,5,3,3]]
[[10,5,3,-2]]
[[10,5,2,1]]
[[10,-3,11]]
    public int ConvertToArray(TreeNode root, int targetSum) {
        ArrayList<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> path = new ArrayList<Integer>();
        ConvertToArray(root, paths, path);
        return 1;
    }
    
    public void ConvertToArray(TreeNode root, ArrayList<ArrayList<Integer>> paths, ArrayList<Integer> path)
    {
        if(root == null)
        {
            var currpath = new ArrayList<>(path);
            paths.add(currpath);
            return;
        }
        
        path.add(root.val);        
        
        ConvertToArray(root.left, paths, path);
        ConvertToArray(root.right, paths, path);
        path.remove(path.size()-1);
    }
}

You can check this implementation.

import java.util.Scanner;
import java.util.Stack;
import java.util.stream.Collectors;

public class Main {
    static class Node {
        int value;
        Node right;
        Node left;

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

        @Override
        public String toString() {
            return "" + value;
        }

        public boolean isLeaf() {
            return right == null && left == null;
        }
    }

    public static void main(String[] args) {
        String input = "10 5 -3 3 2 3 -2 N N N N N 1 N N N 11 N N";
        Scanner scanner = new Scanner(input);
        Node root = new Node(scanner.nextInt());
        parse(scanner, root);
        print(root);
    }

    private static void parse(Scanner scanner, Node node) {
        if (!scanner.hasNext("N")) {
            node.left = new Node(scanner.nextInt());
        } else {
            scanner.next();
        }
        if (!scanner.hasNext("N")) {
            node.right = new Node(scanner.nextInt());
        } else {
            scanner.next();
        }
        if (node.left != null) {
            parse(scanner, node.left);
        }
        if (node.right != null) {
            parse(scanner, node.right);
        }
    }

    private static void print(Node node) {
        Stack<Node> stack = new Stack<>();
        stack.push(node);
        print(node, stack);
    }

    private static void print(Node node, Stack<Node> stack) {
        if (node.isLeaf()) {
            print(stack);
        } else {
            if (node.left != null) {
                stack.push(node.left);
                print(node.left, stack);
            }
            if (node.right != null) {
                stack.push(node.right);
                print(node.right, stack);
            }
        }
        stack.pop();
    }
    
    private static void print(Stack<Node> stack) {
        String values = stack.stream().map(s -> "" + s.value).collect(Collectors.joining(","));
        System.out.println("[[" + values + "]]");
    }
}

Output

[[10,5,3,3]]
[[10,5,3,-2]]
[[10,5,2,1]]
[[10,-3,11]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM