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:

什么才是幸福呢

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

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

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

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

 

 

 

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

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

 

【论文复现】一种基于价格弹性矩阵的居民峰谷分时电价激励策略【需求响应】(Matlab代码实现)内容概要:本文介绍了一种基于价格弹性矩阵的居民峰谷分时电价激励策略,旨在通过需求响应机制优化电力系统的负荷分布。该研究利用Matlab进行代码实现,构建了居民用电行为与电价变动之间的价格弹性模型,通过分析不同时间段电价调整对用户用电习惯的影响,设计合理的峰谷电价方案,引导用户错峰用电,从而实现电网负荷的削峰填谷,提升电力系统运行效率与稳定性。文中详细阐述了价格弹性矩阵的构建方法、优化目标函数的设计以及求解算法的实现过程,并通过仿真验证了所提策略的有效性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力,从事需求响应、电价机制研究或智能电网优化等相关领域的科研人员及研究生。; 使用场景及目标:①研究居民用电行为对电价变化的响应特性;②设计并仿真基于价格弹性矩阵的峰谷分时电价激励策略;③实现需求响应下的电力负荷优化调度;④为电力公司制定科学合理的电价政策提供理论支持和技术工具。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,深入理解价格弹性建模与优化求解过程,同时可参考文中方法拓展至其他需求响应场景,如工业用户、商业楼宇等,进一步提升研究的广度与深度。
### 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); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值