Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
private List<Integer> path;
    private List<List<Integer>> paths;
    @Test
    void ttt() {
        int[][] arr = {{4,3,1}, {3,2,4}, {3}, {4}, {}};
        test(arr);
    }
    // 4,3,1  3,2,4  3  4  []
    public List<List<Integer>> test(int[][] graph) {
        path = new ArrayList<>();
        paths = new ArrayList<>();
        dfs(graph, 0);

        return paths;
    }

    public void dfs(int[][] graph, int current) {
        // 현재 포지션 저장
        path.add(current);
        if (current == graph.length - 1) {
            paths.add(new ArrayList<>(path));
        } else {
            for (final int i : graph[current]) {
                // 재귀
                dfs(graph, i);
            }
        }
        // 수행 후 포지션 뒤로
        path.remove(path.size() - 1);
    }


3. fail

Code Block
class Solution {
    
    List<Integer> result;
    
    public boolean isSameTree(TreeNode p, TreeNode q) {
        result = new ArrayList<>();
        List<Integer> listP = search(p);

        result = new ArrayList<>();
        List<Integer> listQ = search(q);
        return listP.equals(listQ);
        
    }
    
    private List<Integer> search(TreeNode node) {
        result.add(node.val);
        while (Objects.nonNull(node.left)) {
            search(node.left);
        }

        while (Objects.nonNull(node.right)) {
            search(node.right);
        }

        return result;
    }
}