623. Add One Row to Tree
Problem statement
Given the root of a binary tree, then value
v
and depthd
, you need to add a row of nodes with valuev
at the given depthd
. The root node is at depth 1.The adding rule is: given a positive integer depth
d
, for each NOT null tree nodesN
in depthd-1
, create two tree nodes with valuev
asN's
left subtree root and right subtree root. AndN'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 depthd
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 5Example 2:
Input: A binary tree as following: 4 / 2 / \ 3 1 v = 1 d = 3 Output: 4 / 2 / \ 1 1 / \ 3 1Note:
- The given d is in range [1, maximum depth of the given tree + 1].
- The given binary tree has at least one tree node.
Solution
This is the second problem of leetcode weekly contest 37, which is involved with a binary tree. According to what described, obviously, there are two solutions: BFS and DFS.
BFS
This is the BFS problem for a tree, normally, we should get the size of the queue before we process each level, which is different with the BFS in two dimension matrix.
Time complexity is O(n), space complexity is O(n).
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* addOneRow(TreeNode* root, int v, int d) { if(d == 1){ TreeNode* node = new TreeNode(v); node->left = root; return node; } queue<TreeNode*> que; que.push(root); while(!que.empty() && d > 1){ d--; int size = que.size(); while(size > 0){ TreeNode* node = que.front(); que.pop(); if(d == 1){ TreeNode* left_node = new TreeNode(v); left_node->left = node->left; node->left = left_node; TreeNode* right_node = new TreeNode(v); right_node->right = node->right; node->right = right_node; } else { if(node->left){ que.push(node->left); } if(node->right){ que.push(node->right); } } size--; } } return root; } };
DFS
This is the best solution for this problem, it is
More accurate, this is a divide and conquer problem. We conquer first in current level and divide to lower level.
Time complexity is O(n), space complexity is O(1).
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* addOneRow(TreeNode* root, int v, int d) { // at least one root node, no need do root == NULL test if(d == 1){ TreeNode* dummy = new TreeNode(v); dummy->left = root; return dummy; } add_node(root, v, d - 1); return root; } private: void add_node(TreeNode* node, int val, int depth){ // conquer if(node == NULL){ return; } if(depth == 1){ TreeNode* left_node = new TreeNode(val); left_node->left = node->left; node->left = left_node; TreeNode* right_node = new TreeNode(val); right_node->right = node->right; node->right = right_node; return; } // divide add_node(node->left, val, depth - 1); add_node(node->right, val, depth - 1); return; } };