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 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 = 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.
  1. class Solution:
  2. def addOneRow(self, root, v, d):
  3. """
  4. :type root: TreeNode
  5. :type v: int
  6. :type d: int
  7. :rtype: TreeNode
  8. """
  9. if d is 1:
  10. newRoot = TreeNode(v)
  11. newRoot.left = root
  12. return newRoot
  13. queue = [root]
  14. while queue and d > 2:
  15. d -= 1
  16. count = len(queue)
  17. for i in range(count):
  18. node = queue.pop(0)
  19. if node.left:
  20. queue.append(node.left)
  21. if node.right:
  22. queue.append(node.right)
  23. for curNode in queue:
  24. temp = curNode.left
  25. curNode.left = TreeNode(v)
  26. curNode.left.left = temp
  27. temp = curNode.right
  28. curNode.right = TreeNode(v)
  29. curNode.right.right = temp
  30. return root

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8401763.html

时间: 2024-10-09 20:52:09

623. Add One Row to Tree 将一行添加到树中的相关文章

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 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 n

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 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-

[Swift]LeetCode623. 在二叉树中增加一行 | 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 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-

css超出一行添加省略号属性:text-overflow和white-space

css超出一行添加省略号属性:text-overflow和white-space A-A+ 前端博客?前端开发?CSS?14994View1 文章目录 1.text-overflow: ellipsis; 2.white-space属性 3.word-wrap 通过使用text-overflow和white-space属性来使文本在一行内显示,超出则加省略号,添加如下HTML代码: <p>前端开发博客专注前端开发和技术分享,如果描述超过100像素,则会隐藏,添加省略号</p> CS

SQL Server中如何定位Row Lock锁定哪一行数据

在SQL Server中有时候会使用提示(Hint)强制SQL使用行锁(Row Lock),前两天有个同事咨询了一个问题,如何定位Row Lock具体锁定了哪一行.其实这个问题只适合研究一下,实际意义并不大,因为找到.定位被锁定的行的代价开销较大,而意义却不怎么大,而且使用场景也很少.那么下面我们来探讨.研究一下这个问题吧: 在会话窗口(会话ID=65)下执行下面SQL语句,模拟SQL Server使用行锁锁定某一行记录: USE AdventureWorks2012; GO   SELECT 

学习分享 lex tree diffrient row height(tree的行高调节)

Creating a Flex Tree with variable height rows can be mysteriously difficult if you are new to Flex item renderers.  This cookbook will give you the step by step instructions to creating a Tree control with variable height renderers. First you will n

Subquery returns more than 1 row查询结果多于一行

例如 :WHERE(" use_id =(select user_id from store_staff where store_id="+ store_id + ")"); 改为  WHERE(" use_id =any(select user_id from store_staff where store_id="+ store_id + ")");

easyui中导航菜单accordion与tree的动态添加

博客分类: Java Web开发 Js代码   $.parser.parse(); $.ajax({ url:my.bp()+'/main/menuaction!createMenu.action', type:'post', async: false, dataType:'json', success:function(data){ $.each(data, function(i, item){ $('#main_west_accordion').accordion('add',item);

android-ListView 最后一行添加控件

今天在做一个功能的时候,要求必须是在一个listview下,有一段提示行的文字,自己的那个listview的adapter用的是cursoradapter,这样的话,处理布局的灵活性就大打折扣了.最开始的想法是改变item的布局,然后在adapter中处理,保证在listview加载到最后一行的时候,控制item中添加的textview显示,实现提示效果.但是,这时候会面临很多的问题.比如,最后一行点击事件,最后一行的下划线等. 后来,想到了另外一种方法,就是不再adapter中添加,而是直接在