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

    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;
        }
    };
  • 相关阅读:
    B站14天数据分析笔记6次课笔记
    B站14天数据分析笔记5次课作业
    B站14天数据分析笔记5次课Pandas
    1037 在霍格沃茨找零钱 (20 point(s))
    第一章:第三节探索性数据分析
    Java没有运行选项
    Eclipse截图的时候错误提示消失/复制粘贴错误信息
    错误记录_css属性的值一定不需要引号么?
    错误记录_语法哪里错了?
    微软输入法使用
  • 原文地址:https://www.cnblogs.com/wdw828/p/7058067.html
Copyright © 2011-2022 走看看