diff --git a/src/main/java/leetcode/editor/cn/InsertIntoABinarySearchTree.java b/src/main/java/leetcode/editor/cn/InsertIntoABinarySearchTree.java new file mode 100644 index 0000000..cde4312 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/InsertIntoABinarySearchTree.java @@ -0,0 +1,102 @@ +//给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值 +//都不同。 +// +// 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。 +// +// +// +// 示例 1: +// +// +//输入:root = [4,2,7,1,3], val = 5 +//输出:[4,2,7,1,3,5] +//解释:另一个满足题目要求可以通过的树是: +// +// +// +// 示例 2: +// +// +//输入:root = [40,20,60,10,30,50,70], val = 25 +//输出:[40,20,60,10,30,50,70,null,null,25] +// +// +// 示例 3: +// +// +//输入:root = [4,2,7,1,3,null,null,null,null,null,null], val = 5 +//输出:[4,2,7,1,3,5] +// +// +// +// +// +// +// 提示: +// +// +// 给定的树上的节点数介于 0 和 10^4 之间 +// 每个节点都有一个唯一整数值,取值范围从 0 到 10^8 +// -10^8 <= val <= 10^8 +// 新值和原始二叉搜索树中的任意节点值都不同 +// +// Related Topics 树 二叉搜索树 二叉树 👍 229 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.TreeNode; + +//701:二叉搜索树中的插入操作 +class InsertIntoABinarySearchTree { + public static void main(String[] args) { + //测试代码 + Solution solution = new InsertIntoABinarySearchTree().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + + /** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ + class Solution { + public TreeNode insertIntoBST(TreeNode root, int val) { + if (root == null) { + return new TreeNode(val); + } + TreeNode node = root; + while (true) { + if (node.val > val) { + if (node.left == null) { + node.left = new TreeNode(val); + break; + } else { + node = node.left; + } + } else { + if (node.right == null) { + node.right = new TreeNode(val); + break; + } else { + node = node.right; + } + } + } + return root; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/InsertIntoABinarySearchTree.md b/src/main/java/leetcode/editor/cn/doc/content/InsertIntoABinarySearchTree.md new file mode 100644 index 0000000..a504b44 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/InsertIntoABinarySearchTree.md @@ -0,0 +1,42 @@ +

给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。

+ +

注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果

+ +

 

+ +

示例 1:

+ +
+输入:root = [4,2,7,1,3], val = 5
+输出:[4,2,7,1,3,5]
+解释:另一个满足题目要求可以通过的树是:
+
+
+ +

示例 2:

+ +
+输入:root = [40,20,60,10,30,50,70], val = 25
+输出:[40,20,60,10,30,50,70,null,null,25]
+
+ +

示例 3:

+ +
+输入:root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
+输出:[4,2,7,1,3,5]
+
+ +

 

+ +

 

+ +

提示:

+ + +
Related Topics
  • 二叉搜索树
  • 二叉树

  • 👍 229
  • 👎 0
  • \ No newline at end of file