二叉树常见面试题集合



import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * 二叉树刷题集锦
 */
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(){}
    public TreeNode(int val){
        this.val = val;
    }
}
public class MyBinaryTreeDemo {
    /**
     * 前序遍历
     */
    public static void preorderTraversal(TreeNode root){
        if (root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode tmp = stack.pop();
            System.out.print(tmp.val + "->");
            // 先入栈右孩子,再入栈左孩子
            if (tmp.right != null){
                stack.push(tmp.right);
            }
            if (tmp.left != null){
                stack.push(tmp.left);
            }
        }
    }

    /**
     * 中序遍历
     */
    public static void inorderTraversal(TreeNode root){
        if (root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()){
            // 左,根,右
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            System.out.print(cur.val+"->");
            cur = cur.right;
        }
    }

    /**
     * 后序遍历:左右根
     */
    public static void postorderTraversal(TreeNode root){
        if (root == null){
            return;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.push(root);
        while (!stack1.isEmpty()){
            TreeNode tmp = stack1.pop();
            stack2.push(tmp);
            if (tmp.left != null){
                stack1.push(tmp.left);
            }
            if (tmp.right != null){
                stack1.push(tmp.right);
            }
        }
        while (!stack2.isEmpty()){
            System.out.print(stack2.pop().val+"->");
        }
    }

    /**
     * 层次遍历-原始
     */
    public static  void levelTraversal(TreeNode root){
        if (root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            for (int i = 0; i < size; i++){
                TreeNode tmp = queue.poll();
                System.out.print(tmp.val + "->");
                if (tmp.left != null){
                    queue.add(tmp.left);
                }
                if (tmp.right != null){
                    queue.add(tmp.right);
                }
            }
        }
    }

    /**
     *常见二叉树算法题
     */
    /**
     *1. 求二叉树中节点的个数
     */
    public static int getNodeNum(TreeNode root){
        if (root == null){
            return 0;
        }
        int left = getNodeNum(root.left);
        int right = getNodeNum(root.right);
        return left + right + 1;
    }

    /**
     *2. 求二叉树的深度(高度)
     */
    public static int getDepth(TreeNode root){
        if (root == null){
            return 0;
        }
        int depth = Math.max(getDepth(root.left), getDepth(root.right));
        return depth + 1;
    }

    /**
     *3. 求二叉树第K层的节点个数
     */
    public static int getKthLevelNodeNum(TreeNode root, int k){
        if (root == null || k < 1){
            return 0;
        }
        if (k == 1){
            return 1;
        }
        return getKthLevelNodeNum(root.left, k-1) + getKthLevelNodeNum(root.right, k-1);
    }

    /**
     *4. 求二叉树中叶子节点的个数
     */

    public static int getNodeNumLeaf(TreeNode root){
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null){
            return 1;
        }
        return getNodeNumLeaf(root.left) + getNodeNumLeaf(root.right);
    }

    /**
     *5. 判断两颗二叉树是否相同
     */
    public static boolean isSameTree(TreeNode r1, TreeNode r2){
        if (r1 == null && r2 == null){
            return true;
        }else if (r1 == null || r2 == null){
            return false;
        }
        if (r1.val != r2.val){
            return false;
        }
        return isSameTree(r1.left, r2.left) && isSameTree(r1.right, r2.right);
    }

    /**
     *6. 判断二叉树是不是二叉平衡树
     */
    public static boolean isAVLTree(TreeNode root){
        if (root == null){
            return true;
        }
        int depth = Math.abs(getDepth(root.left)-getDepth(root.right));
        if (depth > 1){
            return false;
        }
        return isAVLTree(root.left) && isAVLTree(root.right);
    }

    /**
     *7.求二叉树的镜像
     */
    public static TreeNode mirrorTree(TreeNode root){
        if (root == null){
            return root;
        }
        TreeNode left = mirrorTree(root.right);
        TreeNode right = mirrorTree(root.left);
        root.left = left;
        root.right = right;
        return root;
    }

    /**
     *8.判断两颗二叉树是否为镜像
     */
    public static boolean isMirror(TreeNode r1, TreeNode r2){
        if (r1 == null && r2 == null){
            return true;
        }else if (r1 == null || r2 == null){
            return false;
        }
        if (r1.val != r2.val){
            return false;
        }
        return isMirror(r1.left, r2.right) && isMirror(r1.right, r2.left);
    }

    /**
     *9.求两棵二叉树的公共祖先
     */
    public static TreeNode getLastCommonParent(TreeNode root, TreeNode n1, TreeNode n2){
        if (findNode(root.left, n1)){
            if (findNode(root.right, n2)){
                return root;
            }else{
               return getLastCommonParent(root.left, n1, n2);
            }
        }else {
            if (findNode(root.left, n2)){
                return root;
            }else{
                return getLastCommonParent(root.right, n1, n2);
            }
        }
    }
    // 递归判断一个点是否在树里
    private static boolean findNode(TreeNode root, TreeNode node){
        if (node == null || root == null){
            return false;
        }
        if (root == node){
            return true;
        }
        return findNode(root.left, node) || findNode(root.right, node);
    }

    /**
     *10.判断是否为二叉查找树BST
     * 解法:中序遍历是递增的
     */
    public static boolean isBST(TreeNode root, int pre){
        if (root == null){
            return true;
        }
        boolean left = isBST(root.left, pre);
        if (!left){
            return false;
        }
        if (root.val <= pre){
            return false;
        }
        pre = root.val;
        boolean right = isBST(root.right, pre);
        if (!right){
            return false;
        }
        return true;
    }


    //
    public static void main(String[] args) {
        TreeNode n1 = new TreeNode(6);
        TreeNode n2 = new TreeNode(5);
        TreeNode n3 = new TreeNode(7);
        TreeNode n4 = new TreeNode(2);
        TreeNode n5 = new TreeNode(5);
        TreeNode n6 = new TreeNode(8);
        n1.left = n2;
        n1.right = n3;
        n2.left = n4;
        n2.right = n5;
        n3.right = n6;
        // preorderTraversal(n1);
        // inorderTraversal(n1);
        // postorderTraversal(n1);
        // levelTraversal(n1);
        // System.out.println(getNodeNum(n1));
        // System.out.println(getDepth(n1));
        // System.out.println(getKthLevelNodeNum(n1, 3));
        // System.out.println(getNodeNumLeaf(n1));
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值