leetcode题解-701-二叉搜索树中的插入操作 发表于 2019-12-16 | 更新于 2019-12-19 题目 My way 递归实现 12345678910111213141516171819202122232425262728/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* insertIntoBST(TreeNode* root, int val) { auto node = root; TreeNode* tmp; while (node) { tmp = node; if (node->val == val) return root; if (node->val > val) node = node->left; else node = node->right; } if (tmp->val > val) tmp->left = new TreeNode(val); else tmp->right = new TreeNode(val); return root; }}; 很简单的题目,就不写非递归实现了。