Problem
You are given the root
node of a binary search tree (BST) and a value
to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
Example 1:
1 | Input: root = [4,2,7,1,3], val = 5 |
Example 2:
1 | Input: root = [40,20,60,10,30,50,70], val = 25 |
Example 3:
1 | Input: root = [4,2,7,1,3,null,null,null,null,null,null], val = 5 |
Constraints:
- The number of nodes in the tree will be in the range
[0, 104]
. -108 <= Node.val <= 108
- All the values
Node.val
are unique. -108 <= val <= 108
- It’s guaranteed that
val
does not exist in the original BST.
Analysis
这道题目是二叉树中非常经典的插入题目,在一刻BST中插入元素。思路其实很简单,通过和root比对,递归地去左子树或者右子树寻找,找到之后,新增加一个节点即可。
Solution
因为新增加的节点需要让父节点指向,所以在父节点的时候就进行判断,如果小于root的值且左子树为空,则插入;或者大于root的值且右子树为空,也是插入操作。
Code
1 | /** |
Summary
这道题目对于熟悉二叉树的朋友来说应该是easy的,在这里记录分享一下。这道题的分析到这里,谢谢!