二叉树基础面试题

1. 二叉树的前序遍历。

2. 二叉树中序遍历 。

3. 二叉树的后序遍历 。

4. 检查两颗树是否相同。

5. 另一颗树的子树。

6. 二叉树最大深度。

7. 判断一颗二叉树是否是平衡二叉树。

8. 对称二叉树。

9.层序遍历

10.判断一棵树是不是完全二叉树

//给你二叉树的根节点 root ,返回它节点值的 前序,中序,后序 遍历。把遍历结果放入顺序表中
*/
//层序遍历二叉树(孩子树)
void levelOrderTraversal1(Node root){
    Queue<Node> queue=new LinkedList<>();
    if(root==null){
        return ;
    }
    queue.offer(root);
    while (!queue.isEmpty()){
        Node ret=queue.poll();
        System.out.print(ret.val);
        if(ret.left!=null){
            queue.offer(ret.left);
        }
        if(ret.right!=null){
            queue.offer(ret.right);
        }
    }

}
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();

        if(root==null){
            return result;
        }
        result.add(root.val);
        result.addAll(preorderTraversal(root.left));
        result.addAll(preorderTraversal(root.right));
        return result;

    }
}
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        if(root==null){
            return result;
        }
        result.addAll(inorderTraversal(root.left));
        result.add(root.val);
        result.addAll(inorderTraversal(root.right));
        return result;

    }
}
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        if(root==null){
            return result;
        }
        result.addAll(postorderTraversal(root.left));

        result.addAll(postorderTraversal(root.right));
        result.add(root.val);
        return result;

    }
}
//给定一个二叉树,找出其最大深度。
//二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
//说明: 叶子节点是指没有子节点的节点。
class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        return 1+max(maxDepth(root.left),maxDepth(root.right));


    }
    public int max(int a,int b){
        return a>b ? a:b;
    }
}
//给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。
//如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null&&q==null){
            return true;
        }
        if(p==null||q==null){
            return false;
        }
        if(p.val!=q.val){
            return false;
        }
        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
    }
}
    给你两棵二叉树 root 和 subRoot 。检验 root 中是否包含和 subRoot 具有相同结构和节点值的子树。如果存在,返回 true ;否则,返回 false 。
        二叉树 tree 的一棵子树包括 tree 的某个节点和这个节点的所有后代节点。tree 也可以看做它自身的一棵子树。
class Solution {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null && subRoot == null) {
            return true;
        }
        if (root == null || subRoot == null) {
            return false;
        }
        boolean ret = false;
        if (root.val == subRoot.val) {
            ret = isSameTree(root, subRoot);
        }
        if (!ret) {
            ret = isSubtree(root.right, subRoot);
        }
        if (!ret) {
            ret = isSubtree(root.left, subRoot);
        }
        return ret;


    }
    public  boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        if (p.val != q.val) {
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}
//给定一个二叉树,判断它是否是高度平衡的二叉树。
//本题中,一棵高度平衡二叉树定义为:
//一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        int rootleft=maxDeep(root.left);
        int rootright=maxDeep(root.right);
        if(rootleft-rootright>1||rootleft-rootright<-1){
            return false;
        }
        return isBalanced(root.left)&&isBalanced(root.right);
    }
    public int maxDeep(TreeNode root) {
        if(root==null){
            return 0;
        }
        return 1+max(maxDeep(root.left),maxDeep(root.right));


    }
    public int max(int a,int b){
        return a>b ? a:b;
    }
}
//给定一个二叉树,检查它是否是镜像对称的。
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        return ismid(root.left,root.right);
    }
    public boolean ismid(TreeNode r1,TreeNode r2){
        if(r1==null&&r2==null){
            return true;
        }
        if(r1==null||r2==null){
            return false;
        }
        if(r1.val!=r2.val){
            return false;
        }
        return ismid(r1.left,r2.right)&&ismid(r1.right,r2.left);
    }

}
// 判断一棵树是不是完全二叉树
    boolean isCompleteTree(Node root) {
        Queue<Node> queue = new LinkedList<>();
        if (root == null) {
            return true;
        }
        queue.offer(root);
        boolean step = false;
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            if (!step) {
                if (node.left != null && node.right != null) {
                    queue.offer(node.left);
                    queue.offer(node.right);
                } else if (node.left != null && node.right == null) {
                    queue.offer((node.left));
                    step = true;
                } else if (node.left == null && node.right != null) {
                    return false;
                } else {
                    step = true;
                }
            } else if (step) {
                if (node.left != null || node.right != null) {
                    return false;
                }
            }
        }
        return true;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值