栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

二叉树练习题 — Java

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

二叉树练习题 — Java

检查两颗树是否相同

链接

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null){
            return true;
        }else if(p != null && q == null || p == null && q != null){
            return false;
        }else if(p.val != q.val){
            return false;
        }else{
            return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
        }
    }
}
另一颗树的子树

链接

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

        return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);
    }
}
二叉树最大深度

链接

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int leftHeight = maxDepth(root.left);
        int rightHeight = maxDepth(root.right);

        return(leftHeight <= rightHeight) ? (rightHeight+1) : (leftHeight+1);
    }
}
判断一颗二叉树是否是平衡二叉树

链接

class Solution {

    public int height(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHeight = height(root.left);
        int rightHeight = height(root.right);

        return (leftHeight <= rightHeight) ? (rightHeight+1) : (leftHeight+1);
    }
    public boolean isBalanced(TreeNode root){
        if(root == null){
            return true;
        }
        int left = height(root.left);
        int right = height(root.right);

        return Math.abs(left-right) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }
   
}
// 优化后
class Solution {

    public int height(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHeight = height(root.left);
        int rightHeight = height(root.right);

        if(leftHeight >= 0 && rightHeight >= 0 && Math.abs(leftHeight - rightHeight) <= 1){
            return Math.max(leftHeight,rightHeight)+1;
        }else{//进入else 说明不平衡
            return -1;
        }
    }
    public boolean isBalanced(TreeNode root){
        if(root == null){
            return true;
        }
        return height(root) >= 0;
    }
}
对称二叉树

链接

class Solution {
    public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree){
        if(leftTree == null && rightTree == null){
            return true;
        }
        if(leftTree != null && rightTree == null || leftTree == null && rightTree != null){
            return false;
        }
        if(leftTree.val == rightTree.val){
            return isSymmetricChild(leftTree.left,rightTree.right) && isSymmetricChild(leftTree.right,rightTree.left);
        }
        return false;
    }

    public boolean isSymmetric(TreeNode root){
        if(root == null){
            return false;
        }
        return isSymmetricChild(root.left,root.right);
    }
}
二叉树的构建和遍历

链接

import java.util.*;

class TreeNode{
    char val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {}

    public TreeNode(char val) {
        this.val = val;
    }
    public TreeNode(char val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Main{
    public static int i = 0;
    public static TreeNode creatBinaryTree(String str){
        TreeNode root = null;
        if(str.charAt(i) != '#'){
            root = new TreeNode(str.charAt(i));
            i++;
            root.left = creatBinaryTree(str);
            root.right = creatBinaryTree(str);
        }else{
            i++;
        }
        return root;
    }

    public static void inorder(TreeNode root){
        if(root == null){
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            TreeNode ret = creatBinaryTree(str);
            inorder(ret);
        }
    }
}
二叉树的分层遍历

链接

public void levelOrder(TreeNode root){//一般思路
        if(root == null){
            return;
        }
        Queue queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null){
                System.out.println(cur.val + " ");
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
            }
        }
    }
class Solution {
    public List> levelOrder(TreeNode root) {
        List> ret = new ArrayList<>();
        if(root == null){
            return ret;
        }
        Queue queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();//代表这一层当前有多少节点
            List list = new ArrayList<>();
            while(size != 0){
                TreeNode 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;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1034401.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号