Leetcode-5084 Insufficient Nodes in Root to Leaf Paths(根到叶路径上的不足节点)

这题出的不好

 1 class Solution
 2 {
 3     public:
 4         int go(TreeNode* root, int limit,int k)
 5         {
 6             if(!(root->left) && !(root->right))
 7             {
 8                 int t = k+root->val;
 9                 if(t < limit)
10                 {
11                     return -1;
12                 }
13             }
14             int k1 = 0,k2 = 0;
15             if(root->left)
16                 k1 = go(root->left, limit,k+root->val);
17             if(root->right)
18                 k2 = go(root->right, limit,k+root->val);
19             
20             if(k1==-1)
21                 root->left = NULL;
22             if(k2==-1)
23                 root->right = NULL;
24             if(root->left==NULL && root->right==NULL)
25             {
26                 int t = k+root->val;
27                 if(t < limit)
28                 {
29                     return -1;
30                 }
31             }
32             if(k1==-1 && k2==-1)
33                 return -1;
34             return 1;
35         }
36         TreeNode* sufficientSubset(TreeNode* root, int limit)
37         {
38             if(!root)
39                 return NULL;
40             if(go(root,limit,0)==-1)
41                 return NULL;
42             return root;
43         }
44 };

 

上一篇:[Swift]LeetCode1080. 根到叶路径上的不足节点 | Insufficient Nodes in Root to Leaf Paths


下一篇:5084. Insufficient Nodes in Root to Leaf Paths