【LeetCode】623. Add One Row to Tree 解题报告(Python)

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/add-one-row-to-tree/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:

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

题目大意

新添一层值为v的节点到二叉树的第d层。原来二叉树的该层节点设置成新层的子节点。

解题方法

采用的是递归的解法。其实仔细读题是有很大的作用的,题目中说了很清楚了,如果d是1的话,新建节点,原来的root设成该节点的左边节点。如果d是2的话,就给root节点新建出左右节点,把原来的root子节点设置成新子节点的子节点即可~~

代码:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def addOneRow(self, root, v, d):
"""
:type root: TreeNode
:type v: int
:type d: int
:rtype: TreeNode
"""
if not root: return root
if d == 1:
left = TreeNode(v)
left.left = root
root = left
elif d == 2:
left = TreeNode(v)
right = TreeNode(v)
left.left = root.left
right.right = root.right
root.left = left
root.right = right
else:
self.addOneRow(root.left, v, d - 1)
self.addOneRow(root.right, v, d - 1)
return root

方法二: 循环。

待续。

日期

2018 年 3 月 21 日 ———— 啊,耽误了一天没刷题。。

【LeetCode】623. Add One Row to Tree 解题报告(Python)的更多相关文章

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

    Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...

  2. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  3. 【leetcode】623. Add One Row to Tree

    题目如下: Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with ...

  4. 623. Add One Row to Tree

    Problem statement Given the root of a binary tree, then value v and depth d, you need to add a row o ...

  5. 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  6. 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...

  7. 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  8. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  9. 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...

随机推荐

  1. shell 基本系统维护指令

    笔记 [1]man.passwd.su.echo命令的用法 (1)获取联机帮助 1)使用man命令可以找到特定的联机帮助页,并提供简短的命令说明.一般语法格式为: man commandname 2) ...

  2. Demo03找素数

    package Deom1;import java.awt.*;import java.util.Scanner;public class lx {//输入任意两个正整数,求出这两个正整数之间素数的个 ...

  3. 我的分布式微服务框架:YC-Framework

    YC-Framework官方文档:http://framework.youcongtech.com/ YC-Framework源代码:https://github.com/developers-you ...

  4. 如何使用 Kind 快速创建 K8s 集群?

    作者|段超 来源|尔达 Erda 公众号 ​ 导读:Erda 作为一站式云原生 PaaS 平台,现已面向广大开发者完成 70w+ 核心代码全部开源!在 Erda 开源的同时,我们计划编写<基于 ...

  5. day01 前端bootstrap框架

    day01 django框架之bootstrap框架 今日内容概要 前端框架之bootstrap 该框架支持cv编写前端页面 利用socket模块编写一个简易版本的web框架 利用wsgiref模块编 ...

  6. AI作曲的一个点子

    通常的AI作曲都是通过拆分音乐为几个声道, 然后再把各个声道拆成音符去分析. 我忽然之间有个想法,是否可以继续拆分下去. 音符就是一些有规则的高低电平,这样把音符拆成电平. 一定会带来巨大的运算,但如 ...

  7. C++构造函数和析构函数初步认识

    构造函数 1.构造函数与类名相同,是特殊的公有成员函数.2.构造函数无函数返回类型说明,实际上构造函数是有返回值的,其返回值类型即为构造函数所构建到的对象.3.当新对象被建立时,构造函数便被自动调用, ...

  8. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(五)-文件管理初步介绍

    其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...

  9. Lombok安装及Spring Boot集成Lombok

    文章目录 Lombok有什么用 使用Lombok时需要注意的点 Lombok的安装 spring boot集成Lombok Lombok常用注解 @NonNull @Cleanup @Getter/@ ...

  10. 一份不错的Java就业指导

    想要成为合格的Java程序员或工程师到底需要具备哪些专业技能,面试者在面试之前到底需要准备哪些东西呢? 本文陈列的这些内容既可以作为个人简历中的内容,也可以作为面试的时候跟面试官聊的东西,你可以把这些 ...