class Solution { public: TreeNode* insertIntoBST(TreeNode* root, int val) { TreeNode* m=root; TreeNode* x=root; while(root) { x=root; if(valval) { root=root->left; } else { root=root->right; } } TreeNode* a=new(TreeNode); a->val=val; if(!m) { return a; } if(val val) { x->left=a; } else { x->right=a; } return m; } };
做得有点慢,效果一般。
一直在想这么插入一个新点。
加油!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)