**Leetcode 701. Insert into a Binary Search Tree
Posted Z-Pilgrim
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了**Leetcode 701. Insert into a Binary Search Tree相关的知识,希望对你有一定的参考价值。
https://leetcode.com/problems/insert-into-a-binary-search-tree/description/
想清楚很好写。。
/**
* 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:
void dfs(TreeNode* root, TreeNode* pre, int val)
if (!root)
if (pre && pre->val < val) pre->right = new TreeNode(val);
else if (pre && pre->val > val) pre->left = new TreeNode(val);
else root = new TreeNode(val);
return;
if (root->val > val)
dfs(root->left, root, val);
if (root->val < val)
dfs(root->right, root, val);
TreeNode* insertIntoBST(TreeNode* root, int val)
dfs(root, NULL, val);
return root;
;
以上是关于**Leetcode 701. Insert into a Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode] 701. Insert into a Binary Search Tree
leetcode701. Insert into a Binary Search Tree
Leetcode 701. Insert into a Binary Search Tree