二叉树的题目

目录

1、将遍历的结果放在list中

2、判断两棵树是否相同

3、翻转二叉树

4、判断平衡二叉树

5、判断二叉树是否对称

6、判断是否为完全二叉树


先创建一个二叉树

public class BinaryTree {

    static class TreeNode {

        public char val;

        public TreeNode left;

        public TreeNode right;


        public TreeNode(char val) {

            this.val = val;

        }

    }


    public TreeNode creatTree() {

        TreeNode A = new TreeNode('A');

        TreeNode B = new TreeNode('B');

        TreeNode C = new TreeNode('C');

        TreeNode D = new TreeNode('D');

        TreeNode E = new TreeNode('E');

        TreeNode F = new TreeNode('F');

        TreeNode G = new TreeNode('G');

        TreeNode H = new TreeNode('H');


        A.left = B;

        A.right = C;

        B.left = D;

        B.right = E;

        C.left = F;

        C.right = G;

        E.right = H;


        return A;

    }

1、将遍历的结果放在list中

将前序遍历的结果存储在list中

public class Solution {

    public List<Character>preorderTraversal(BinaryTree.TreeNode root){

        List<Character>list=new ArrayList<>(); //创建一个List

        if (root==null){

            return list;

        }

        list.add(root.val); //将根放入


        List<Character>leftTree=preorderTraversal(root.left); //根的左子树

        list.addAll(leftTree);


        List<Character>rightTree=preorderTraversal(root.right); //根的右子树

        list.addAll(rightTree);


        return list;

    }

}

2、判断两棵树是否相同

要判断两棵树是否相同,要同时判断根是否相同,然后判断两棵树的左子树是否相同和右子树是否相同。

public class Solution {

    boolean isSameTree(BinaryTree.TreeNode p, BinaryTree.TreeNode q){

        if ((p!=null&&q==null)||(p==null&&q!=null)){

            return false;

        }

        if (p==null&&q==null){

            return true;

        }

        if (p.val!= q.val){

            return false;

        }

        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);

    }

}

3、翻转二叉树

class Solution{

    public BinaryTree.TreeNode invertTree(BinaryTree.TreeNode root){

        if (root==null){

            return null;

        }

        if (root.left==null && root.right==null){

            return root;

        }

        BinaryTree.TreeNode tmp=root.left;

        root.left=root.right;

        root.right=tmp;

        invertTree(root.left);

        invertTree(root.right);


        return root;

    }

}

4、判断平衡二叉树

class Solution{

    public boolean isBalance(BinaryTree.TreeNode root){

        if (root==null){

            return true;

        }

        int leftHeight=getHeight(root.left);

        int rightHeight=getHeight(root.right);


    return Math.abs(leftHeigh-rightHeigh<1&&isBalance(root.left)&&isBalance(root.right);

    }

    //求二叉树的高度

    int getHeight(BinaryTree.TreeNode root){

        if (root==null){

            return 0;

        }

        int leftHeightDepth=getHeight(root.left);

        int rightHeightDepth=getHeight(root.right);


        return leftHeightDepth>rightHeightDepth?leftHeightDepth+1:rightHeightDepth+1;

    }

}

5、判断二叉树是否对称

class Solution{

    public boolean isSymmetric(BinaryTree.TreeNode root){

        if (root==null){

            return true;

        }

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

    }

    private boolean isSymmetricChild(BinaryTree.TreeNode leftTree, BinaryTree.TreeNode rightTree){

        if ((leftTree!=null&&rightTree==null)||(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);

    }

}

6、判断是否为完全二叉树

class Solution{

    boolean isCompleteTree(BinaryTree.TreeNode root){

        if (root==null){

            return true;

        }

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

        queue.offer(root);


        while (!queue.isEmpty()){

            BinaryTree.TreeNode cur=queue.poll();

            if (cur!=null){

                queue.offer(cur.left);

                queue.offer(cur.right);

            }else {

                break;

            }

        }

        while (!queue.isEmpty()){

            BinaryTree.TreeNode tmp=queue.peek();

            if (tmp==null){

                queue.poll();

            }else {

                return false;

            }

        }

        return true;

    }

}

  • 21
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值