Java集合与数据结构——二叉树03

Posted RAIN 7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java集合与数据结构——二叉树03相关的知识,希望对你有一定的参考价值。

文章目录

对称二叉树

/**
 * 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 isSymmetricChild(TreeNode leftTree,TreeNode rightTree)
        if(leftTree == null && rightTree != null)
            return false;
        
        if(leftTree != null && rightTree == null)
            return false;
        
        if(leftTree == null && rightTree == null)
            return true;
        
        if(leftTree.val != rightTree.val)
            return false;
        

        return isSymmetricChild(leftTree.left,rightTree.right)
            && isSymmetricChild(leftTree.right,rightTree.left);


    public boolean isSymmetric(TreeNode root) 
          if(root == null)
               return true;
          

          return isSymmetricChild(root.left,root.right);
    

二叉树的镜像

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x)  val = x; 
 * 
 */
class Solution 
    public TreeNode mirrorTree(TreeNode root) 
       if(root == null)
          return root;
       

       if(root.left == null && root.right == null)
           return root;
       

       TreeNode tmp = root.left;
       root.left = root.right;
       root.right = tmp;
      
      if(root.left != null)
          mirrorTree(root.left);
      
      
      if(root.right != null)
           mirrorTree(root.right);
      
      

       return root;
    

二叉树的层序遍历

01


    void levelOrderTravelsal(TreeNode root)
        if(root == null)
            return ;
        

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty())

                TreeNode top = queue.poll();
            System.out.print(top.val+" ");
                if(top.left != null)
                    queue.offer(top.left);
                

                if(top.right != null)
                    queue.offer(top.right);
                
        
        System.out.println();
    


实现效果

02

我们在这里看到的层序遍历还和之前我们做的不一样,我们来看结果,发现二叉树的每一层分别进行分行输出。


 /**
 * 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) 
            
       //  List 链表的作用只是为了满足结果的打印要求
        List<List<Integer>> ret = new ArrayList<>();

        if(root == null)
            return ret ;
        

        // 队列的作用是用来实现层序遍历的
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);


        while(!queue.isEmpty())

            int size = queue.size();
            List<Integer> list = new ArrayList<>();

            while(size !=0)
                TreeNode top = queue.poll();
                lists.add(top.val);
                if(top.left != null)
                    queue.offer(top.left);
                

                if(top.right != null)
                    queue.offer(top.right);
                

                size--;
            

             ret.add(list);
        
          
    return ret;
 
    

判断一个二叉树是否为完全二叉树

利用之前的层序遍历,一层一层的遍历,当 top 为null 时,遍历停止,之后如果之后的队列中元素全部都是 null 的话 则返回true,这棵树是完全二叉树,如果这是队列中有一个元素不是 null,则返回false,这棵树不是完全二叉树.


    public boolean isCompleteTree(TreeNode root) 
        if(root == null)
            return true;
        

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty())
            TreeNode top = queue.poll();
            if(top != null)
                queue.offer(top.left);
                queue.offer(top.right);
            else
                break;   
            
          
        
        
        while(!queue.isEmpty())
            TreeNode cur = queue.peek();
            if(cur == null)
                queue.poll();
            else
                return false;
            
        



        return true;
    

二叉树的拓展问题

以上是关于Java集合与数据结构——二叉树03的主要内容,如果未能解决你的问题,请参考以下文章

Java集合与数据结构——二叉树01

Java集合与数据结构 二叉树

Java集合与数据结构 二叉树

Java集合与数据结构 二叉树

Java集合与数据结构 优先级队列堆

Java集合与数据结构 优先级队列堆