NC372. 插入二叉搜索树
描述
示例1
输入:
{2,1,3},4
输出:
{2,1,3,#,#,#,4}
C 解法, 执行用时: 55ms, 内存消耗: 10572KB, 提交时间: 2022-05-23
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param val int整型 * @return TreeNode类 */ typedef struct TreeNode*Bitree; struct TreeNode* insertToBST(struct TreeNode* root, int val ) { // write code here Bitree parent,p; p=root; parent=p; while(p!=NULL) { parent=p; if(p->val==val) return root; p=p->val>val?p->left:p->right; } p=(Bitree)malloc(sizeof(struct TreeNode)); p->val=val; p->left=p->right=NULL; if(root==NULL) return p; if(parent->val>val) parent->left=p; else parent->right=p; return root; }
C++ 解法, 执行用时: 55ms, 内存消耗: 10588KB, 提交时间: 2022-08-06
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ 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: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param val int整型 * @return TreeNode类 */ TreeNode* insertToBST(TreeNode* root, int val) { // write code here if(!root) { TreeNode* node=new TreeNode(val); return node; } TreeNode* c=root; TreeNode* pre=root; while(c) { pre=c; if(c->val>val) c=c->left; else c=c->right; } TreeNode *node=new TreeNode(val); if(val<pre->val) pre->left=node; else pre->right=node; return root; } };
C++ 解法, 执行用时: 56ms, 内存消耗: 10588KB, 提交时间: 2022-05-29
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: TreeNode* insertToBST(TreeNode* root, int val) { if(!root) { TreeNode* node=new TreeNode(val); return node; } TreeNode* c=root; TreeNode* pre=root; while(c) { pre=c; if(c->val>val) c=c->left; else c=c->right; } TreeNode* node=new TreeNode(val); if(val<pre->val) pre->left=node; else pre->right=node; return root; } };
C++ 解法, 执行用时: 58ms, 内存消耗: 10572KB, 提交时间: 2022-07-06
/** * 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类 * @param val int整型 * @return TreeNode类 */ TreeNode* insertToBST(TreeNode* root, int val) { // write code here TreeNode * node= new TreeNode(val); if(root == nullptr) { root = node; return root; } TreeNode* pre = root; TreeNode* cur = root; while(cur) { pre = cur; if(val < cur->val) cur = cur->left; else if(val == cur->val) break; else cur = cur->right; } if(!cur) { if(val < pre->val) pre->left = node; else pre->right = node; } return root; } };
C++ 解法, 执行用时: 58ms, 内存消耗: 10588KB, 提交时间: 2022-07-13
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: TreeNode* insertToBST(TreeNode* root, int val) { if(!root) { TreeNode* node=new TreeNode(val); return node; } TreeNode* c=root; TreeNode* pre=root; while(c) { pre=c; if(c->val>val) c=c->left; else c=c->right; } TreeNode* node=new TreeNode(val); if(val<pre->val) pre->left=node; else pre->right=node; return root; } };