题目:

Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with valuev 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 nodesN 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, itsoriginal right subtree should be the right subtree of the new right subtree root. If depthd is 1 that means there is no depth d-1 at all, then create a tree node with valuev 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 = 1d = 2Output: 4/ \1   1/     \2       6/ \     / 3   1   5   

Example 2:

Input:
A binary tree as following:4/   2    / \   3   1    v = 1d = 3Output: 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.

分析(原创):

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public TreeNode addOneRow(TreeNode root, int v, int d) {//给定二叉树,在指定的深度d添加指定的值v,并且v作为新的左右子树节点//思路:记录当前节点的深度,当count==d-1时,则进行节点生成,以及对新节点继承父节点的值。//注意:如果d为1,则只需要把根节点作为其右子树返回即可return addOneRow(root,v,d,1);}public TreeNode addOneRow(TreeNode root, int v, int d,int lastDepth){if(root==null) return root;if(d==1){TreeNode newRoot=new TreeNode(v);newRoot.left=root;return newRoot;}if(curDepth(root,lastDepth)==d-1){//如果刚好判断到d-1层,则后面节点进行拼接TreeNode tempLeft=new TreeNode(v);if(root.left!=null){tempLeft.left=root.left;}TreeNode tempRight=new TreeNode(v);if(root.right!=null){tempRight.right=root.right;}root.left=tempLeft;root.right=tempRight;}else{root.left=addOneRow(root.left,v,d,lastDepth+1);root.right=addOneRow(root.right,v,d,lastDepth+1);}return root;}//找到当前节点的层数public int curDepth(TreeNode root,int lastDepth){return root==null?1+lastDepth:lastDepth;}
}

LeetCode.623 Add One Row to Tree相关推荐

  1. [leetcode]623. Add One Row to Tree

    [leetcode]623. Add One Row to Tree Analysis 今天要吃柚子-- [每天刷题并不难0.0] Given the root of a binary tree, t ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. LeetCode之Add Two Numbers

    LeetCode之Add Two Numbers 题目:You are given two linked lists representing two non-negative numbers. Th ...

  4. LeetCode 445. Add Two Numbers II

    LeetCode 445. Add Two Numbers II Solution1:我的答案 利用了栈,这样就不用翻转链表了... /*** Definition for singly-linked ...

  5. 【注意】LeetCode 2. Add Two Numbers

    LeetCode 2. Add Two Numbers 这种沙比提怎么都写不对了??? Solution1:学习这种写法 /*** Definition for singly-linked list. ...

  6. [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值

    You need to find the largest value in each row of a binary tree. Example: Input: 1/ \3 2/ \ \ 5 3 9 ...

  7. 【LeetCode】513. Find Bottom Left Tree Value

    问题描述 问题链接:https://leetcode.com/problems/find-bottom-left-tree-value/#/description Given a binary tre ...

  8. leetcode 662. Maximum Width of Binary Tree | 662. 二叉树最大宽度(BFS)

    题目 https://leetcode.com/problems/maximum-width-of-binary-tree/ 题解 本题思路来源于二叉树的层序遍历. 层序遍历类似问题:leetcode ...

  9. LeetCode 1516. Move Sub-Tree of N-Ary Tree(DFS)

    文章目录 1. 题目 2. 解题 1. 题目 Given the root of an N-ary tree of unique values, and two nodes of the tree p ...

最新文章

  1. php自动处理,thinkphp中的三种自动处理
  2. ubuntu 18.10无法locate boot-repair
  3. 改变变压器联接方式可消除某些特定次数的谐波_电工牛人10年经验,总结的4电工常用接线方法41例,电机、变压器、接触器..都有...
  4. 【干货】网易严选大数据架构.pdf(附下载链接)
  5. idea中build project不能用_Java语言编程第40讲——如何在一个项目中组织多个SpringBoot服务
  6. 可见面判别算法---深度排序算法
  7. hustoj的搭建(最新踩坑)
  8. K8S coreDNS部署及简单验证
  9. 【硬件木马项目】第二篇:硬件木马检测方法的种类及原理
  10. JavaScript常用库和API学习文档
  11. 渗透测试-网页接口加密暴破
  12. Jenkins和Docker在HULK的落地实践
  13. 三角函数:正弦余弦定理及应用
  14. 最新:亚马逊运营思路
  15. 阿里云服务器ECS由什么组成
  16. 如何查看html的字体,如何检测网页中使用了哪种定义的字体?
  17. OSPF与BGP联动
  18. 腾讯T2大牛亲自教你!5214页PDF的进阶架构师学习笔记,终局之战
  19. 自然数、整数、有理数、实数、无理数
  20. h5背景图片尺寸怎么设置_CSS3中background-size实现背景图片大小可自定义的几种效果(代码实例 )...

热门文章

  1. 好系统教你Win7系统开机启动慢怎么解决?
  2. Elasticsearch 分布式搜索引擎 -- 自动补全(拼音分词器、自定义分词器、自动补全查询、实现搜索框自动补全)
  3. 电脑计算机硬盘怎么加,电脑怎么增加硬盘内存
  4. 中秋节快到了,一起用MATLAB绘制一款2.5D月饼叭
  5. 《CSS实战案例汇总》涟漪
  6. 关于Open函数的newline参数
  7. 阿里云邮箱25端口被关闭,改用465端口Java发送邮件
  8. Python之组合数据类型(列表、元组、集合、字典)
  9. 十几年稳坐“大哥”位,搞Java的程序员就是这么“牛x”!
  10. 大数据学习之HBase入门笔记