leetcode之DFS+BFS+DSU刷题总结2

Posted nuist__NJUPT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode之DFS+BFS+DSU刷题总结2相关的知识,希望对你有一定的参考价值。

leetcode之DFS+BFS+DSU刷题总结2

1-对称二叉树
题目链接:题目链接戳这里!!!

思路1:迭代法
一棵二叉树,左右子树分别进队,如果左右子树都为空,则结束遍历,如果左右子树仅一个为空,或左右子树根节点不同,则返回false,否则 ,按照左节点的左子树,右节点的右子树,左节点的右子树,右节点的左子树顺序入队。

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public boolean isSymmetric(TreeNode root) 
        if(root==null)
            return true ;
        
        Queue<TreeNode> queue = new LinkedList<>() ;
     
        queue.add(root.left) ;
        queue.add(root.right) ;

        while(!queue.isEmpty())
            TreeNode node1 = queue.poll() ;
            TreeNode node2 = queue.poll() ;
            if(node1 == null && node2 == null)
                continue ;
            
            if(node1==null || node2==null || node1.val != node2.val)
                return false ;
            
            queue.add(node1.left) ;
            queue.add(node2.right) ;
            queue.add(node1.right) ;
            queue.add(node2.left) ;
        
        return true ;
    


思路2:递归法
和思路1方法一样,不过没有用到队列,直接递归比较。

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public boolean isSymmetric(TreeNode root) 
        if(root==null)
            return true ;
        
        return f(root.left, root.right) ;
    
    public boolean f(TreeNode node1, TreeNode node2)
        if(node1==null && node2==null)
            return true ;
        
        if(node1==null || node2==null || node1.val != node2.val)
            return false ;
        
        return f(node1.left,node2.right) && f(node1.right,node2.left) ;
    


2-二叉树的层次遍历
题目链接:题目链接戳这里!!!

思路1:bfs
借助队列,首先让根节点入队,只要队不空,计算队列的大小,每次将每一层的根节点放入集合,同时将左右子树入队。

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public List<List<Integer>> levelOrder(TreeNode root) 
        if(root==null)
            return new ArrayList<>() ;
        
        List<List<Integer>> ans = new ArrayList<>() ;
        Queue<TreeNode> queue = new LinkedList<>() ;

        queue.add(root) ;
        while(!queue.isEmpty())
            int size = queue.size() ;
            List<Integer> list = new ArrayList<>() ;
            while(size>0)
                TreeNode node = queue.poll() ;
                list.add(node.val) ;
                if(node.left != null)
                    queue.add(node.left) ;
                
                if(node.right != null)
                    queue.add(node.right) ;
                
                size -- ;
            
           ans.add(list) ;
        
        return ans ;

    


3-二叉树的锯齿形遍历
题目链接:题目链接戳这里!!!

思路:bfs+翻转
和层次遍历思路一样,只不过对于偶数层的元素,存入集合后,将奇数元素翻转即可。

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) 
        if(root==null)
            return new ArrayList<>() ;
        
        List<List<Integer>> ans = new ArrayList<>() ;
        Queue<TreeNode> queue = new LinkedList<>() ;
        
        queue.add(root) ;
        while(!queue.isEmpty())
            int size = queue.size() ;
            boolean level = true ;
            List<Integer> list = new ArrayList<>() ;
            while(size>0)
                TreeNode node = queue.poll() ;
                list.add(node.val) ;
                if(node.left != null)
                    queue.add(node.left) ;
                
                if(node.right != null)
                    queue.add(node.right) ;
                 
                size -- ;
            
            if(ans.size()%2!=0)
                Collections.reverse(list) ;
            
            ans.add(list) ;
          
        
        return ans ;
    


4-二叉树的最大深度
题目链接:题目链接戳这里!!!

思路1:递归法

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public int maxDepth(TreeNode root) 
        if(root==null)
            return 0 ;
        
        return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1 ;
    


思路2:bfs
使用队列进行层次遍历,没遍历一层二叉树的高度height加1.

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public int maxDepth(TreeNode root) 
        if(root==null)
            return 0 ;
        
       int height = 0 ;
       Queue<TreeNode>  queue = new LinkedList<>() ;
       queue.add(root) ;

       while(!queue.isEmpty())
           int size = queue.size();
           height ++ ;
           for(int i=0; i<size; i++)
               TreeNode node = queue.poll() ;
               if(node.left != null)
                   queue.add(node.left) ;
               
               if(node.right != null)
                   queue.add(node.right) ;
               
           
       
       return height ;
    


5-二叉树的层序遍历II
题目链接:题目链接戳这里!!!

思路:bfs
bfs进行层次遍历,对每一层的遍历结果插入到结果集合的头部。

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public List<List<Integer>> levelOrderBottom(TreeNode root) 
        if(root==null)
            return new ArrayList<>() ;
        
        LinkedList<List<Integer>> ans = new LinkedList<>() ;
        Queue<TreeNode> queue = new LinkedList<>() ;
        queue.add(root) ;

        while(!queue.isEmpty())
            int size = queue.size() ;
            List<Integer> temp = new LinkedList<>() ;
            for(int i=0; i<size; i++)
                TreeNode node = queue.poll() ;
                temp.add(node.val) ;
                if(node.left != null)
                    queue.add(node.left) ;
                
                if(node.right != null)
                    queue.add(node.right) ;
                
            
           ans.addFirst(temp) ; 
        
        return ans ;

    

6-二叉树的最小深度
题目链接:题目链接戳这里!!!

思路1:dfs法

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public int minDepth(TreeNode root) 
        if(root==null)
            return 0 ;
        
        if(root.left==null && root.right != null) //根节点的左子树为空,只需考虑右子树
            return minDepth(root.right) + 1 ;
        
        if(root.right==null && root.left!=null)//根节点的右子树为空,只需考虑左子树
            return minDepth(root.left) + 1 ;
        
        return Math.min(minDepth(root.left),minDepth(root.right)) + 1  ;
    


思路2:bfs
将根节点进队,如果队不空,依次层次遍历,如果某一层左右子树都为空,则结束遍历,否则继续遍历。

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public int minDepth(TreeNode root) 
        if(root==null)
            return 0 ; 
        
       Queue<TreeNode> queue = new LinkedList<>() ;
       queue.add(root) ;
       int height = 0 ;
       while(!queue.isEmpty())
           int size = queue.size() ;
           height ++ ;
           for(int i=0; i<size; i++)
               TreeNode node = queue.poll() ;
               if(node.left==null && node.right==null)
                   return height ;
               
               if(node.left!=null)
                   queue.add(node.left) ;
               
               if(node.right!=null)
                   queue.add(node.right) ;
               
           
       
       return height ;
    


7-最小高度树
题目链接:题目链接戳这里!!!

思路:拓朴排序+bfs

我们从边缘开始,先找到所有出度为1的节点,然后把所有出度为1的节点进队列,然后不断地bfs,最后找到的就是两边同时向中间靠近的节点,那么这个中间节点就相当于把整个距离二分了,那么它当然就是到两边距离最小的点啦,也就是到其他叶子节点最近的节点了。

class Solution 
    public List<Integer> findMinHeightTrees(int n, int[][] edges) 
     
        List<Integer> ans = new ArrayList<>();
        Queue<Integer> queue = new LinkedList<>() ;
        List<List<Integer>> adj = new ArrayList<>() ;
        if(n==1)
            ans.add(0) ;
            return ans ;
        
        int [] degree = new int [n] ; //记录出度
        for(int i=0; i<n; i++)
            adj.add(new ArrayList<>()) ;
        

        for(int []edge : edges)
            degree[edge[0]] ++ ;
            degree[edge[1]] ++ ;
            adj.get(edge[0]).add(edge[1]) ;
            adj.get(edge[1]).add(edge[0]) ;
        
        for(int i=0; i<n; i++)
            if(degree[i]==1)
                queue.add(ileetcode之最短路径+记忆化dfs+bfs+动态规划刷题总结

LeetCode刷题之BFS和DFS

LeetCode刷题记录

LeetCode刷题记录

leetcode刷题总结301-350

LeetCode刷题记录