深度优先搜索DFS

深度优先搜索DFS

它的思想是从一个顶点开始,沿着一条路一直走到底,如果发现不能到达目标解,那就返回到上一个节点,然后从另一条路开始走到底,这种尽量往深处走的概念即是深度优先的概念。

深度优先搜索一般通过栈来实现。

二叉树的先序遍历:LeetCode144

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        LinkedList<TreeNode> stack = new LinkedList();
        List<Integer> res = new ArrayList();
        //根、左、右
        while(!stack.isEmpty() || root != null) {
            if(root != null) {
                res.add(root.val);
                stack.offer(root);
                root = root.left;
            } else {
                root = stack.pollLast();
                root = root.right;
            }
        }
        return res;
    }
}

二叉树的中序遍历:LeetCode94

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        LinkedList<TreeNode> stack = new LinkedList();
        List<Integer> res = new ArrayList();
        //左、根、右
        while(!stack.isEmpty() || root != null) {
            if(root != null) {
                stack.offer(root);
                root = root.left;
            } else {
                root = stack.pollLast();
                res.add(root.val);
                root = root.right;
            }
        }
        return res;
    }
}

二叉树的后序遍历:LeetCode145

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        LinkedList<Integer> res = new LinkedList();
        LinkedList<TreeNode> stack = new LinkedList();
        //根、右、左的倒序
        while(!stack.isEmpty() || root != null) {
            if(root != null) {
                res.offerFirst(root.val);
                stack.offer(root);
                root = root.right;
            } else {
                root = stack.pollLast();
                root = root.left;
            }
        }
        return res;
    }
}

也可以用过回溯来实现。

不同路径III:LeetCode980

在二维网格 grid 上,有 4 种类型的方格:

1 表示起始方格。且只有一个起始方格。
2 表示结束方格,且只有一个结束方格。
0 表示我们可以走过的空方格。
-1 表示我们无法跨越的障碍。
返回在四个方向(上、下、左、右)上行走时,从起始方格到结束方格的不同路径的数目,每一个无障碍方格都要通过一次。

class Solution {
    boolean[][] isUsed;
    int count;
    public int uniquePathsIII(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        isUsed = new boolean[m][n];
        int startm = 0, startn = 0;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(grid[i][j] == 0)
                    isUsed[i][j] = false;
                else if(grid[i][j] == -1)
                    isUsed[i][j] = true;
                else if(grid[i][j] == 1) {
                    startm = i;
                    startn = j;
                    isUsed[i][j] = false;
                } else
                    isUsed[i][j] = true;
            }
        }
        count = 0;
        dfs(grid, startm, startn);
        return count;
    }
    private void dfs(int[][] grid, int curm, int curn) {
        isUsed[curm][curn] = true;
        if(curm > 0) {
            if(grid[curm - 1][curn] == 2) {
                if(allUsed())
                    count++;
            } else if(!isUsed[curm - 1][curn])
                dfs(grid, curm - 1, curn);
        }
        if(curn > 0) {
            if(grid[curm][curn - 1] == 2) {
                if(allUsed())
                    count++;
            } else if(!isUsed[curm][curn - 1])
                dfs(grid, curm, curn - 1);
        }        
        if(curm < grid.length - 1) {
            if(grid[curm + 1][curn] == 2) {
                if(allUsed())
                    count++;
            } else if(!isUsed[curm + 1][curn])
                dfs(grid, curm + 1, curn);
        }        
        if(curn < grid[0].length - 1) {
            if(grid[curm][curn + 1] == 2) {
                if(allUsed())
                    count++;
            } else if(!isUsed[curm][curn + 1])
                dfs(grid, curm, curn + 1);
        }
        isUsed[curm][curn] = false;
    }
    private boolean allUsed() {
        for(int i = 0; i < isUsed.length; i++) {
            for(int j = 0; j < isUsed[0].length; j++) {
                if(!isUsed[i][j])
                    return false;
            }
        }
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值