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;
        }
    };
  • 相关阅读:
    发一段非万能的sql分页代码
    手机版的首页不能翻页,用的是UC7.1
    关于Enterprise library logging中一个没搞明白的东西,希望有大大们进来帮忙释疑一下, THKS
    关于CodeSmith主模板与子模板同时从一个基类继承后的参数传值问题(提供模板下载)
    golang 去除字符串首尾空格
    构建一个SDK的打印信息
    PeekMessage抓取消息,如何把每个消息都获取到呢?
    DirectDraw学习 之 入门(二)
    20111121 —— wince6.0 BSP 里面加了 locale 后编译 failed 之解决办法
    DirectDraw学习 之 编译自带的sample代码
  • 原文地址:https://www.cnblogs.com/wdw828/p/7058067.html
Copyright © 2011-2022 走看看