zoukankan      html  css  js  c++  java
  • [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-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.

    在二叉树中增加一行。

    给定一个二叉树,根节点为第1层,深度为 1。在其第 d 层追加一行值为 v 的节点。

    添加规则:给定一个深度值 d (正整数),针对深度为 d-1 层的每一非空节点 N,为 N 创建两个值为 v 的左子树和右子树。

    将 N 原先的左子树,连接为新节点 v 的左子树;将 N 原先的右子树,连接为新节点 v 的右子树。

    如果 d 的值为 1,深度 d - 1 不存在,则创建一个新的根节点 v,原先的整棵树将作为 v 的左子树。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/add-one-row-to-tree
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这道题我提供两种做法,BFS和DFS,思路是一样的,都是从根节点往下,一直遍历到 d - 1 层,然后把新的节点加进去。

    首先是BFS,我们用queue遍历到目标层 d 的上一层 d - 1,然后对着d - 1 层,先把他的左孩子右孩子用temp指针保存起来,再把需要增加的节点加进去。注意唯一的corner case就是如果新的节点需要加在第一层的话,返回的就是新的根节点了。

    时间O(n)

    空间O(n)

    Java实现

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public TreeNode addOneRow(TreeNode root, int v, int d) {
    12         // corner case
    13         if (d == 1) {
    14             TreeNode newRoot = new TreeNode(v);
    15             newRoot.left = root;
    16             return newRoot;
    17         }
    18 
    19         // normal case
    20         // BFS遍历node一直到d - 1层
    21         Queue<TreeNode> queue = new LinkedList<>();
    22         queue.offer(root);
    23         for (int i = 1; i < d - 1; i++) {
    24             int size = queue.size();
    25             for (int j = 0; j < size; j++) {
    26                 TreeNode cur = queue.poll();
    27                 if (cur.left != null) {
    28                     queue.offer(cur.left);
    29                 }
    30                 if (cur.right != null) {
    31                     queue.offer(cur.right);
    32                 }
    33             }
    34         }
    35 
    36         // 把新一层的node接在第d - 1和第d层之间
    37         while (!queue.isEmpty()) {
    38             TreeNode cur = queue.poll();
    39             TreeNode temp = cur.left;
    40             cur.left = new TreeNode(v);
    41             cur.left.left = temp;
    42             temp = cur.right;
    43             cur.right = new TreeNode(v);
    44             cur.right.right = temp;
    45         }
    46         return root;
    47     }
    48 }

    DFS,可以直接参见代码。

    时间O(n)

    空间O(n)

    Java实现

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public TreeNode addOneRow(TreeNode root, int v, int d) {
    12         // corner case
    13         if (d == 1) {
    14             TreeNode newRoot = new TreeNode(v);
    15             newRoot.left = root;
    16             return newRoot;
    17         }
    18         // normal case
    19         helper(v, root, 1, d);
    20         return root;
    21     }
    22 
    23     private void helper(int val, TreeNode root, int curDepth, int d) {
    24         if (root == null) {
    25             return;
    26         }
    27         if (curDepth == d - 1) {
    28             TreeNode temp = root.left;
    29             root.left = new TreeNode(val);
    30             root.left.left = temp;
    31             temp = root.right;
    32             root.right = new TreeNode(val);
    33             root.right.right = temp;
    34         } else {
    35             helper(val, root.left, curDepth + 1, d);
    36             helper(val, root.right, curDepth + 1, d);
    37         }
    38     }
    39 }

    LeetCode 题目总结

  • 相关阅读:
    UILabel 设置字体间的距离 和 行与行间的距离
    IB_DESIGNABLE 和 IBInspectable 的使用
    干货博客
    GitHub克隆速度太慢解决方案
    实时(RTC)时钟,系统时钟和CPU时钟
    折腾了好久的vscode配置c/c++语言环境(Windows环境下)
    c语言中的malloc函数
    记录一下关于在工具类中更新UI使用RunOnUiThread犯的极其愚蠢的错误
    记录关于Android多线程的一个坑
    Android中限制输入框最大输入长度
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14509266.html
Copyright © 2011-2022 走看看