diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /delete_leaves_given_value.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'delete_leaves_given_value.c')
-rw-r--r-- | delete_leaves_given_value.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/delete_leaves_given_value.c b/delete_leaves_given_value.c new file mode 100644 index 0000000..1ad048e --- /dev/null +++ b/delete_leaves_given_value.c @@ -0,0 +1,25 @@ +// 1325. Delete Leaves With a Given Value +#include "leetcode.h" + +/* + * given a binary tree 'root' and an integer 'target', delete all the leaf nodes + * with value 'target'. note that once you delete a leaf node with value + * 'target', if its parent node becomes a leaf node and has the value 'target', + * it should also be deleted (you need to continue doing that until you cannot) + */ + +struct TreeNode { + int val; + struct TreeNode *left; + struct TreeNode *right; +}; + +struct TreeNode *removeLeafNodes(struct TreeNode *root, int target) { + if (!root) + return NULL; + root->left = removeLeafNodes(root->left, target); + root->right = removeLeafNodes(root->right, target); + if (!root->left && !root->right && root->val == target) + return NULL; + return root; +} |