Java:数据结构-二叉树oj题

1.判断两个数是否相同

题目链接:. - 力扣(LeetCode)

public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null && q!=null || q==null && p!=null){
            return false;
        }
        if(p==null && q==null){
            return true;
        }
        if(q.val!=p.val){
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }

 2.另一颗的子树

题目链接:. - 力扣(LeetCode)

class Solution {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root==null){
            return false;
        }
        if(isSameTree(root,subRoot)){
            return true;
        }
        if(isSubtree(root.left,subRoot)){;
        return true;
        }
        if(isSubtree(root.right,subRoot)){;
        return true;
        }
        return false;
        
    }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null && q!=null || q==null && p!=null){
            return false;
        }
        if(p==null && q==null){
            return true;
        }
        if(q.val!=p.val){
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }
}

3.翻转二叉树

题目链接:. - 力扣(LeetCode)

public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        TreeNode tem=null;
        tem=root.left;
        root.left=root.right;
        root.right=tem;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

4.对称二叉树

题目链接:. - 力扣(LeetCode)

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        return isChild(root.left,root.right);
        
    }
    public boolean isChild(TreeNode a1,TreeNode a2){
        if(a1==null && a2!=null || a1!=null && a2==null){
            return false;
        }
        if(a1==null && a2==null){
            return true;
        }
        if(a1.val!=a2.val ){
            return false;
        }
        return isChild(a1.left,a2.right)&&isChild(a1.right,a2.left);
    }

 5.平衡二叉树

题目链接:. - 力扣(LeetCode)

public int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftHeight=getHeight(root.left);
        if(leftHeight<0){
            return -1;
        }
        int rightHeight=getHeight(root.right);
        if(rightHeight>=0 && Math.abs(leftHeight-rightHeight)<=1){
            return Math.max(leftHeight,rightHeight)+1;
        }else {
            return -1;
        }
    }

6.二叉树的遍历

题目链接:二叉树遍历_牛客题霸_牛客网 

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            String str=in.nextLine();
            TreeNode root=creatTree(str);
            inorderTree(root);
        }
    }
    public static int i=0;
    public static TreeNode creatTree(String str){
        TreeNode root=null;
        if(str.charAt(i)!='#'){
            root=new TreeNode(str.charAt(i));
            i++;
            root.left=creatTree(str);
            root.right=creatTree(str);
        }else{
            i++;
        }
        return root;

    }
    public static void inorderTree(TreeNode root){
        if(root==null){
            return ;
        }
        inorderTree(root.left);
        System.out.print(root.val+" ");
        inorderTree(root.right);
    }

7.二叉树的分层遍历

题目链接:. - 力扣(LeetCode)

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret=new ArrayList<>();
        if(root==null){
            return ret;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.empty()){
            int size=queue.size();
            List<Character> list=new List<>();
            while(size!=0){
                TreeNode<Character> cur=queue.poll();
                list.add(cur.val);
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }    
        return ret;
    }
}

希望能对大家有所帮助!!!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值