[leetcode] 623. Add One Row to Tree

本文介绍了一种在二叉树特定深度增加一行节点的方法,通过递归算法实现。示例展示了如何在深度为2和3的位置插入值为1的节点,改变了二叉树的结构。代码段提供了C++实现,适用于二叉树节点操作。

Description

Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value v at the given depth d. The root node is at depth 1.

The adding rule is: given a positive integer depth d, for each NOT null tree nodes N in depth d-1, create two tree nodes with value v as N’s left subtree root and right subtree root. And N’s original left subtree should be the left subtree of the new left subtree root, its original right subtree should be the right subtree of the new right subtree root. If depth d is 1 that means there is no depth d-1 at all, then create a tree node with value v as the new root of the whole original tree, and the original tree is the new root’s left subtree.

Example 1:

Input: 
A binary tree as following:
       4
     /   \
    2     6
   / \   / 
  3   1 5   

v = 1

d = 2

Output: 
       4
      / \
     1   1
    /     \
   2       6
  / \     / 
 3   1   5   

Example 2:

Input: 
A binary tree as following:
      4
     /   
    2    
   / \   
  3   1    

v = 1

d = 3

Output: 
      4
     /   
    2
   / \    
  1   1
 /     \  
3       1

Note:

  1. The given d is in range [1, maximum depth of the given tree + 1].
  2. The given binary tree has at least one tree node.

分析

  • 这是一个递归的题,我觉得如果没接触这题的话,可能做不出来,注意看d=1和d=0的妙用和处理,d=1 处理左子节点,d=0 处理右子节点
  • 如果要理解过程的话,手动模拟一遍最好

代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* addOneRow(TreeNode* root, int v, int d) {
       if(d==0||d==1){
           TreeNode* newNode=new TreeNode(v);
           (d ? newNode->left:newNode->right)=root;
           return newNode;
       }
        if(root&&d>1){
            root->left=addOneRow(root->left,v,d>2 ? d-1:1);
            root->right=addOneRow(root->right,v, d>2 ? d-1:0);
        }
        return root;
    }
   
};

参考文献

[LeetCode] Add One Row to Tree 二叉树中增加一行

基于CNN+Transformer的图像质量评估python源码+项目说明(如清晰度评分).zip【备注】1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。欢迎下载!欢迎交流学习!不清楚的可以私信问我!基于CNN+Transformer的图像质量评估python源码+项目说明(如清晰度评分).zip无关美学特征,依据清晰度等进行评分主要是在CNN中间层,引入Transformer来学习更多的全局特征背景在各种图像数据不断生成的生活中,人们需要在大量的图像中挑选出可以使用的、符合自己要求的图像,使用算法/深度学习来解决,可以缓解人力不足的问题。数据集图像质量评估数据集有着许多公开数据,这里采用公开数据进行实验。需要下载相应地公开数据才能使用LIVE数据集KONIQ数据集CSIQ数据集LIVEC数据集BID数据集模型回归预测模型,通过对CNN引入Transformer来提升模型性能。CNN对于局部视野有着良好的性能,但是图像的评分不仅仅依据局部内容,也需要对整体内容进行学习。因此通过Transformer加入更多的全局特征来提升模型性能。优化器采用的Adam优化器,方便训练模型依赖pytorch模型运行```nohup python train.py --dataset=livec --train_test_num=5 --cudas=0 --use_seed --random_single > /dev/null 2> e.txt &```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值