随笔 - 363, 文章 - 0, 评论 - 2, 阅读 - 23万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

笔试02

Posted on   心默默言  阅读(148)  评论(0编辑  收藏  举报

1.求二叉树的深度

https://www.cnblogs.com/xudong-bupt/p/4036190.html

复制代码
class TreeNode {
    char val;
    TreeNode left = null;
    TreeNode right = null;

    TreeNode(char _val) {
        this.val = _val;
    }
}
复制代码

这个可以使用递归,分别求出左子树的深度、右子树的深度,两个深度的较大值+1即可。

复制代码
// 获取最大深度
    public static int getMaxDepth(TreeNode root) {
        if (root == null)
            return 0;
        else {
            int left = getMaxDepth(root.left);
            int right = getMaxDepth(root.right);
            return 1 + Math.max(left, right);
        }
    }
复制代码

2.二叉树宽度

  使用队列,层次遍历二叉树。在上一层遍历完成后,下一层的所有节点已经放到队列中,此时队列中的元素个数就是下一层的宽度。以此类推,依次遍历下一层即可求出二叉树的最大宽度。

复制代码
// 获取最大宽度
    public static int getMaxWidth(TreeNode root) {
        if (root == null)
            return 0;

        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
        int maxWitdth = 1; // 最大宽度
        queue.add(root); // 入队

        while (true) {
            int len = queue.size(); // 当前层的节点个数
            if (len == 0)
                break;
            while (len > 0) {// 如果当前层,还有节点
                TreeNode t = queue.poll();
                len--;
                if (t.left != null)
                    queue.add(t.left); // 下一层节点入队
                if (t.right != null)
                    queue.add(t.right);// 下一层节点入队
            }
            maxWitdth = Math.max(maxWitdth, queue.size());
        }
        return maxWitdth;
    }
复制代码

3.求一个二叉树是否为平衡树

复制代码
    /
     * 求树的深度
     * @param root
     * @return
     */
    public static int TreeDepth(BinaryTreeNode root){
        if(root == null) {
            return 0;
        }
        int left = TreeDepth(root.left);
        int right = TreeDepth(root.right);
        return (left > right) ? (left+1) : (right+1);
    }
复制代码
复制代码
    /**
     * 一般解法
     * @param root
     * @return
     */
    public static boolean isBalanced(BinaryTreeNode root){
        if(root == null) {
            return true;
        }
        int left = TreeDepth(root.left);
        int right = TreeDepth(root.right);
        int diff = left-right;
        if(diff > 1 || diff < -1){
            return false;
        }
        return isBalanced(root.left) && isBalanced(root.right);
    }
复制代码

4.求一个数的平方根

复制代码
package interview.squareroot;

public class SquareRoot {

    public static void main(String[] args) {
        double x = 0;
        double A = 3;
        double eta = 0.001;
        while (true) {
            double loss = 0.5 * (A - Math.pow(x, 2));
            System.out.println("loss:"+loss);
            if (loss < 0.001)
                break;
            x = x + eta * (A - x);
        }
        System.out.println("3的平方根是" + x);
    }
}
复制代码

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示