701. 二叉搜索树中的插入操作 - 力扣(LeetCode)
根据二叉搜素树的特性遍历
/**
* 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) {
TreeNode t = new TreeNode(val);
if(root == null){
return t;
}
if(root.val < val){
root.right = insertIntoBST(root.right,val);
}
if(root.val > val){
root.left=insertIntoBST(root.left,val);
}
return root;
}
}