NC169. 修剪叶子
描述
o / \ o o / \ / \ o o o o
示例1
输入:
{1,1,1,1,1,1,1}
输出:
{1}
说明:
示例2
输入:
{1,#,1,#,1,#,1,#,1}
输出:
{1,#,1,#,1}
说明:
C++ 解法, 执行用时: 21ms, 内存消耗: 6660KB, 提交时间: 2022-02-23
static const auto io_sync_off = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return nullptr; }(); class Solution { public: //判断是否叶子节点 bool isleave(TreeNode *root){ if(!root) return false; if(!root->left && !root->right) return true; return false; } TreeNode* pruneLeaves(TreeNode* root) { if(!root) return nullptr; if(isleave(root->left) || isleave(root->right)) root=nullptr; if(root){ root->left = pruneLeaves(root->left); root->right = pruneLeaves(root->right); } return root; } };
C++ 解法, 执行用时: 21ms, 内存消耗: 7052KB, 提交时间: 2021-11-26
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return TreeNode类 */ TreeNode* pruneLeaves(TreeNode* root) { // write code here if (root == nullptr) { return nullptr; } else if (root->left != nullptr && root->left->right == nullptr && root->left->left == nullptr) { return nullptr; } else if (root->right != nullptr && root->right->right == nullptr && root->right->left == nullptr) { return nullptr; } root->left = pruneLeaves(root->left); root->right = pruneLeaves(root->right); return root; } };
C++ 解法, 执行用时: 22ms, 内存消耗: 6664KB, 提交时间: 2022-05-08
static const auto io_sync_off = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return nullptr; } (); class Solution { public: bool isleave(TreeNode* root) { if (!root) return false; if (!root->left && !root->right) return true; return false; } TreeNode* pruneLeaves(TreeNode* root) { if (!root) return nullptr; if (isleave(root->left) || isleave(root->right)) root = nullptr; if (root) { root->left = pruneLeaves(root->left); root->right = pruneLeaves(root->right); } return root; } };
C++ 解法, 执行用时: 22ms, 内存消耗: 6680KB, 提交时间: 2021-12-21
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return TreeNode类 */ TreeNode* pruneLeaves(TreeNode* root) { // write code here if (root == NULL) return NULL; if (root->left == NULL && root->right == NULL) return root; if (isLeaf(root->left) || isLeaf(root->right)) return NULL; root->left = pruneLeaves(root->left); root->right = pruneLeaves(root->right); return root; } bool isLeaf(TreeNode* node) { if (node == NULL) return false; return node->left == NULL && node->right == NULL; } };
C++ 解法, 执行用时: 22ms, 内存消耗: 7092KB, 提交时间: 2022-05-27
static const auto io_sync_off = [](){ std::ios::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return nullptr; }(); class Solution { public: bool isLeave(TreeNode* root) { if(!root) return false; if(!root->left&&!root->right) return true; return false; } TreeNode* pruneLeaves(TreeNode* root) { if(!root) return NULL; if(isLeave(root->left)||isLeave(root->right)) root=NULL; if(root) { root->left=pruneLeaves(root->left); root->right=pruneLeaves(root->right); } return root; } };