面试手撕代码全集(树)

本文涵盖了二叉树的各种面试题目,包括:树的深度、节点总数、遍历方式、判断两棵树是否相同或对称、最近公共祖先、最短路径、第K大/小节点、镜像反转、重建二叉树、子结构判断、序列化与反序列化等。重点讲解了递归和二叉搜索树的特性在解题中的应用。
摘要由CSDN通过智能技术生成

更多见手撕代码全集

7. 二叉树

定义二叉树,二叉树的计算大部分都用递归方法实现,包括深度、前序、中序、后序等

 //定义二叉树节点
public static class TreeNode {
   
    int value;  //数据域  可为其他类型
    TreeNode leftNode;  //左节点
    TreeNode rightNode;  //右节点
    
    public TreeNode(int value, TreeNode leftNode, TreeNode rightNode) {
   
        this.value = value;
        this.leftNode = leftNode;
        this.rightNode = rightNode;
    }

    public TreeNode(int value) {
   
        this.value = value;
        this.leftNode = null;
        this.rightNode = null;
    }
    // value、leftNode、rightNode节点的Getter、Setter被省略
}
//定义二叉树类
public static class BinaryTree {
   
    TreeNode root;

    public BinaryTree(TreeNode root) {
   
        this.root = root;
    }
}

7.1 二叉树深度(递归)

public int getDeep(TreeNode root) {
   
    int deep = 0;
    if (root != null) {
   
        int leftDeep = getDeep(root.leftNode);
        int rightDeep = getDeep(root.rightNode);
        deep = leftDeep > rightDeep ? 1 + leftDeep : 1 + rightDeep;
        //or
        // deep = 1+Math.max(getDeep(root.leftNode),getDeep(root.rightNode));
    }
    return deep;
}

7.2 二叉树节点总数、第K层节点数(递归)

//计算树节点总数
public int getNodeNum(TreeNode root) {
     
    int num = 0;
    if (root != null) {
   
        num = getNodeNum(root.leftNode) + getNodeNum(root.rightNode) + 1;
    }
    return num;
}

//第K层 节点数   递归
public int getKNum(TreeNode root, int k) {
   
    if (root == null) 
        return 0;
    if (k == 0) {
   
        return 1;
    } else {
   
        int leftNum = getKNum(root.leftNode, k - 1);
        int rightNum = getKNum(root.rightNode, k - 1);
        return (leftNum + rightNum);
    }
}
//第K层 节点数   非递归
public int getkNum(TreeNode root, int k) {
   
    if(root == null)
        return 0;
    if( k == 0)
        return 1;
    LinkedList<TreeNode> queue = new LinkedList<>(); 
    queue.offer(root);
    TreeNode node = null;
    int size = 0, i = 0;
    while(!queue.isEmpty() && i<=k) {
   
        size = queue.size();
        for (int j = 0; j < size; j++) {
   
            node = queue.poll();
            if (node.leftNode != null)
                queue.offer(node.leftNode);
            if (node.rightNode != null)
                queue.offer(node.rightNode);
        }
        i++;
    }
    return size;
}

7.3 前序、中序、后序遍历

//递归  前序、中序、后序遍历  取决于不同位置取值
public void preOrder(TreeNode root) {
   
    if (root != null) {
   
        //位置1(前序遍历)System.out.print(root.value + " ");
        preOrder(root.leftNode);
        //位置2 (中序遍历) System.out.print(root.value + " ");
        preOrder(root.rightNode);
        //位置3 (后序遍历) System.out.print(root.value + " ");
    }
}

//非递归  前序(位置1取值)、 中序(位置2取值)遍历
public void NPreOrder(TreeNode root) {
   
    if (root == null)
        return;
    Stack<TreeNode> stack = new Stack<>();
    TreeNode node = root;
    while (!stack.isEmpty() || node != null) {
   
        if (node != null) {
   
            //位置1(前序遍历)   System.out.print(node.value + " ");
            stack.push(node);
            node = node.leftNode;
        } else {
   
            node = stack.pop();
            //位置2(中序遍历)  System.out.print(node.value + " ");
            node = node.rightNode;
        }
    }
}
//非递归  后序遍历
public void NPostOrder(TreeNode root) {
   
    if(root 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值