Using Depth First Search Algorithm to Delete Tree Nodes with Sum

  • 时间:2020-09-10 13:27:27
  • 分类:网络文摘
  • 阅读:116 次

A tree rooted at node 0 is given as follows:

The number of nodes is nodes;
The value of the i-th node is value[i];
The parent of the i-th node is parent[i].
Remove every subtree whose sum of values of nodes is zero.

After doing so, return the number of nodes remaining in the tree.

Input: nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-1]
Output: 2

binary-tree-delete-nodes-with-zero-sum-sub-tree-300x261 Using Depth First Search Algorithm to Delete Tree Nodes with Sum Zero in the Sub Tree algorithms c / c++ recursive

binary-tree-delete-nodes-with-zero-sum-sub-tree

Constraints:
1 <= nodes <= 10^4
-10^5 <= value[i] <= 10^5
parent.length == nodes
parent[0] == -1 which indicates that 0 is the root.

Hints:
Traverse the tree using depth first search.
Find for every node the sum of values of its sub-tree.
Traverse the tree again from the root and return once you reach a node with zero sum of values in its sub-tree.

How to Delete Nodes from Binary Tree using Depth First Search Algorithm?

The binary tree is given in two arrays, we can convert it using the adjacent graph – two dimension array or vectors. Then we can run a DFS (Depth First Search) algorithm that travels the tree from root to leaves recursively.

Bottom-up, from the leaves, we accumulate the sum and the count of the nodes (remaining). At any point, if the sum is zero, we set the counter to zero – which will be bubbled up as the recursion unrolls.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
    int deleteTreeNodes(int nodes, vector<int>& parent, vector<int>& value) {
        vector<vector<int>> g(nodes, vector<int>(0));
        for (int i = 0; i < nodes; ++ i) {
            if (parent[i] == -1) continue;
            g[parent[i]].push_back(i);
        }
        return dfs(g, 0, value)[1];
    }
private:
    vector<int> dfs(const vector<vector<int>> &g, int root, const vector<int>& value) {
        int sum = value[root];
        int remain = 1;
        for (const auto &child: g[root]) {
            vector<int> r = dfs(g, child, value);
            remain += r[1];
            sum += r[0];
        }
        if (sum == 0) remain = 0;
        return {sum, remain};
    }
};
class Solution {
public:
    int deleteTreeNodes(int nodes, vector<int>& parent, vector<int>& value) {
        vector<vector<int>> g(nodes, vector<int>(0));
        for (int i = 0; i < nodes; ++ i) {
            if (parent[i] == -1) continue;
            g[parent[i]].push_back(i);
        }
        return dfs(g, 0, value)[1];
    }
private:
    vector<int> dfs(const vector<vector<int>> &g, int root, const vector<int>& value) {
        int sum = value[root];
        int remain = 1;
        for (const auto &child: g[root]) {
            vector<int> r = dfs(g, child, value);
            remain += r[1];
            sum += r[0];
        }
        if (sum == 0) remain = 0;
        return {sum, remain};
    }
};

The time complexity is O(N) where N is the number of the nodes in the binary tree. The space complexity is also O(N) due to the stack in recursion.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
数学题:神舟五号飞行轨道的近地点高度为200km  数学题:汽车在途中停了一小时,客车速度比汽车慢  数学题:东西南北两条路交叉成直角  数学题:哥哥和弟弟进行100米赛跑  数学题:把14分成若干个自然数的和  数学题:张王李赵刘5人合作完成一项工程  数学题:姐姐8年后的年龄是妹妹3年前的5倍  数学题:一个直角三角形以它的斜边为轴旋转一周  数学题:一个三角形被一个长方形挡住了  摘桃子的数学题 
评论列表
添加评论