leetcode day2

本文深入探讨了二叉树的多种算法实现,包括对称树判断、最大深度查找、层次遍历、平衡树验证及排序数组转换为平衡二叉搜索树等核心问题,提供了递归与迭代两种解决方案。

1 Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

 

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3

 

Note:
Bonus points if you could solve it both recursively and iteratively.

 

talk is cheap let me show you code

递归:

class Solution {

   

    public boolean isSymmetric(TreeNode root) {

        return isMirror(root,root);

    }

    private boolean isMirror(TreeNode left, TreeNode right) {

        if (left ==null && right ==null) {

            return true;

        }

        if (left == null || right == null) {

            return false;

        }

        return left.val == right.val && isMirror(left.left,right.right) && isMirror(left.right,right.left);

    }

}

非递归:

public boolean isSymmetric2(TreeNode root) {

    Queue<TreeNode> queue = new LinkedList<>();

    queue.add(root);

    queue.add(root);

    while (!queue.isEmpty()) {

        TreeNode t1 = ((LinkedList<TreeNode>) queue).pop();

        TreeNode t2 = ((LinkedList<TreeNode>) queue).pop();

        if (t1 ==null && t2 == nullcontinue;

        if (t1 == null || t2 == nullreturn false;

        if (t1.val != t2.val) return false;

        queue.add(t1.left);

        queue.add(t2.right);

        queue.add(t1.right);

        queue.add(t2.left);

    }

 

    return true;

}

 

昨天的 same tree

非递归:

public boolean isSameTree(TreeNode p,TreeNode q) {

    Queue<TreeNode> queue = new LinkedList<>();

     queue.add(p);

     queue.add(q);

     while (!queue.isEmpty()) {

         TreeNode t1 = ((LinkedList<TreeNode>) queue).pop();

         TreeNode t2 = ((LinkedList<TreeNode>) queue).pop();

         if (t1 == null && t2 == nullcontinue;

         if (t1 == null || t2 == nullreturn false;

         if (t1.val != t2.val) return false;

         queue.add(t1.left);

         queue.add(t2.left);

         queue.add(t1.right);

         queue.add(t2.right);

     }

 

     return true;

 

 

}

 

104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its depth = 3.

Accepted

408,537

Submissions

707,797

 

talk is cheap let me show you the code

public int maxDepth(TreeNode root) {

    if (root == null) {

        return 0;

    }

   int leftDepth =  maxDepth(root.left);

   int rightDepth = maxDepth(root.right);

   return leftDepth > rightDepth ? leftDepth+1 : rightDepth+1;

 

}

 

 

107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

 

return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]

 

Accepted

193,422

Submissions

435,269

talk is cheap let me show you the code

public List<List<Integer>> levelOrderBottom(TreeNode root) {

 

    Queue<TreeNode> queue = new LinkedList<>();

    List<List<Integer>> allList = new ArrayList<>();

    queue.add(root);

    while (!queue.isEmpty()) {

        List<Integer> levelList = new ArrayList<>();

        int queueSize = queue.size();

        for (int i=0; i< queueSize; i++) {

            TreeNode node =  ((LinkedList<TreeNode>) queue).pop();

            if (node !=null) {

                levelList.add(node.val);

 

 

                if (node.left != null) {

                    queue.add(node.left);

 

                }

                if (node.right != null) {

                    queue.add(node.right);

                }

            }

        }

        if (!levelList.isEmpty())

           allList.add(levelList);

         }

 

    Collections.reverse(allList);

    return allList;

}

 

108. Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
     / \
   -3   9
   /   /
 -10  5

Accepted

215,250

Submissions

452,314

 

talk is cheap let me show you the code

class Solution {

   public TreeNode sortedArrayToBST(int[] nums) {

        if (nums == null || nums.length == 0) {

            return null;

        }

 

       return bst(nums,0,nums.length-1);

    }

    public TreeNode bst(int [] nums,int begin,int end) {

        if (begin > end ) {

            return null;

        }

        int middle = begin + (end - begin)/2;

        TreeNode treeNode = new TreeNode(nums[middle]);

        treeNode.left = bst(nums,begin,middle-1);

        treeNode.right = bst(nums,middle+1,end);

        return treeNode;

    }

}

 

110. Balanced Binary Tree

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
   / \
  9  20
    /  \
   15   7

Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
      / \
     2   2
    / \
   3   3
  / \
 4   4

Return false.

Accepted

270,541

Submissions

682,299

talk is cheap show you me the code

/**

 * Definition for a binary tree node.

 * public class TreeNode {

 *     int val;

 *     TreeNode left;

 *     TreeNode right;

 *     TreeNode(int x) { val = x; }

 * }

 */

class Solution {

    public boolean isBalanced(TreeNode root) {

        if (root == null) {

            return true;

        }

        int leftDepth = depthTree(root.left);

        int rightDepth = depthTree(root.right);

        return Math.abs(leftDepth - rightDepth) <=1 && isBalanced(root.left) && isBalanced(root.right);

    }

    public int depthTree (TreeNode root) {

        if (root == null) {

            return 0;

        }

        int leftDepth = depthTree(root.left);

        int rightDepth = depthTree(root.right);

        return leftDepth > rightDepth ? leftDepth+1 : rightDepth+1;

    }

}

 

ps:

什么才是幸福呢

有的人拥有的很少,却看似很多

有的人明明拥有很多,却看似很少

反而拥有很少的那个却很幸福,后者却不幸福

为什么呢,是拥有多的不知足,还是拥有少的太容易满足?

 

 

 

昨天听妈妈说 一很亲的邻居去打工,爬很高的楼被砸死了,听了以后心情异常的沉重,小时候常去他家买东西,很热情,而且与父亲的关系很好,我心情沉重了好久,什么才是幸福,可能活的长久,没病没灾

吃饱喝足就是很大的幸福吧,没了生命,不能活着,何谈好好生活呢,珍惜自己所拥有的,快快乐乐的,看似拥有的很好,但是满满的幸福在心里,能说拥有的少吗

 

**项目概述:** 本资源提供了一套采用Vue.js与JavaScript技术栈构建的古籍文献文字检测与识别系统的完整源代码及相关项目文档。当前系统版本为`v4.0+`,基于`vue-cli`脚手架工具开发。 **环境配置与运行指引:** 1. **获取项目文件**后,进入项目主目录。 2. 执行依赖安装命令: ```bash npm install ``` 若网络环境导致安装缓慢,可通过指定镜像源加速: ```bash npm install --registry=https://registry.npm.taobao.org ``` 3. 启动本地开发服务器: ```bash npm run dev ``` 启动后,可在浏览器中查看运行效果。 **构建与部署:** - 生成测试环境产物: ```bash npm run build:stage ``` - 生成生产环境优化版本: ```bash npm run build:prod ``` **辅助操作命令:** - 预览构建后效果: ```bash npm run preview ``` - 结合资源分析报告预览: ```bash npm run preview -- --report ``` - 代码质量检查与自动修复: ```bash npm run lint npm run lint -- --fix ``` **适用说明:** 本系统代码经过完整功能验证,运行稳定可靠。适用于计算机科学、人工智能、电子信息工程等相关专业的高校师生、研究人员及开发人员,可用于学术研究、课程实践、毕业设计或项目原型开发。使用者可在现有基础上进行功能扩展或定制修改,以满足特定应用场景需求。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
【EI复现】基于阶梯碳交易的含P2G-CCS耦合和燃气掺氢的虚拟电厂优化调度(Matlab代码实现)内容概要:本文介绍了基于阶梯碳交易机制的虚拟电厂优化调度模型,重点研究了包含P2G-CCS(电转气-碳捕集与封存)耦合技术和燃气掺氢技术的综合能源系统在Matlab平台上的仿真与代码实现。该模型充分考虑碳排放约束与阶梯式碳交易成本,通过优化虚拟电厂内部多种能源设备的协同运行,提升能源利用效率并降低碳排放。文中详细阐述了系统架构、数学建模、目标函数构建(涵盖经济性与环保性)、约束条件处理及求解方法,并依托YALMIP工具包调用求解器进行实例验证,实现了科研级复现。此外,文档附带网盘资源链接,提供完整代码与相关资料支持进一步学习与拓展。; 适合人群:具备一定电力系统、优化理论及Matlab编程基础的研究生、科研人员或从事综合能源系统、低碳调度方向的工程技术人员;熟悉YALMIP和常用优化算法者更佳。; 使用场景及目标:①学习和复现EI级别关于虚拟电厂低碳优化调度的学术论文;②掌握P2G-CCS、燃气掺氢等新型低碳技术在电力系统中的建模与应用;③理解阶梯碳交易机制对调度决策的影响;④实践基于Matlab/YALMIP的混合整数线性规划或非线性规划问题建模与求解流程。; 阅读建议:建议结合提供的网盘资源,先通读文档理解整体思路,再逐步调试代码,重点关注模型构建与代码实现之间的映射关系;可尝试修改参数、结构或引入新的约束条件以深化理解并拓展应用场景。
### LeetCode 积分获取方式 在 LeetCode 平台上,用户可以通过多种途径来增加自己的积分。完成每日题目能够使用户的分数有所增长[^1]。例如,在提到的一个实例中,“今日得分:+10 总得分:240”,这表明通过解决特定问题可以获得额外的积分奖励。 除了日常挑战外,参与周赛也是提升积分的有效手段之一。每周的比赛不仅考验参赛者的编程技巧,还提供了赢得更高排名的机会,从而带来更多的积分收益。不过需要注意的是,并未直接提及周赛的具体加分机制。 对于那些希望快速提高自己积分的人来说,专注于解答高难度的问题可能是一个不错的选择。通常来说,越难的任务完成后所给予的经验值也会相应增多。然而具体每道题目的确切分值并未在此处给出说明。 另外值得注意的是,持续活跃于社区讨论区也可能间接帮助个人积累更多积分。虽然这种活动本身并不直接贡献于积分系统,但是积极参与可以促进技能成长并发现更高效的解法路径,进而有助于更好地应对各类竞赛和练习中的难题。 ```sql -- 这里提供了一个SQL查询的例子用于计算玩家留存率,而非直接关联到积分制度, -- 但展示了如何利用数据库操作分析平台上的行为数据。 SELECT ROUND(COUNT(DISTINCT player_id) / (SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction FROM Activity WHERE (player_id, DATE_SUB(event_date, INTERVAL 1 DAY)) IN ( SELECT player_id, MIN(event_date) FROM Activity GROUP BY player_id); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值