[LeetCode] 623. Add One Row to Tree

Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.

Note that the root node is at depth 1.

The adding rule is:

  • Given the integer depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.
  • cur's original left subtree should be the left subtree of the new left subtree root.
  • cur's original right subtree should be the right subtree of the new right subtree root.
  • If depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.

Example 1:

Input: root = [4,2,6,3,1,5], val = 1, depth = 2
Output: [4,1,1,2,null,null,6,3,1,5]

Example 2:

Input: root = [4,2,null,3,1], val = 1, depth = 3
Output: [4,2,null,1,1,3,null,null,1]

Constraints:

  • The number of nodes in the tree is in the range [1, 104].
  • The depth of the tree is in the range [1, 104].
  • -100 <= Node.val <= 100
  • -105 <= val <= 105
  • 1 <= depth <= the depth of tree + 1

在二叉树中增加一行。

给定一个二叉树的根 root 和两个整数 val 和 depth ,在给定的深度 depth 处添加一个值为 val 的节点行。

注意,根节点 root 位于深度 1 。

加法规则如下:

给定整数 depth,对于深度为 depth - 1 的每个非空树节点 cur ,创建两个值为 val 的树节点作为 cur 的左子树根和右子树根。
cur 原来的左子树应该是新的左子树根的左子树。
cur 原来的右子树应该是新的右子树根的右子树。
如果 depth == 1 意味着 depth - 1 根本没有深度,那么创建一个树节点,值 val 作为整个原始树的新根,而原始树就是新根的左子树。

来源:力扣(LeetCode)
链接:https://leetcode.cn/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 题目总结

posted @ 2021-03-10 03:37  CNoodle  阅读(54)  评论(0编辑  收藏  举报