/**
* 在BST中插入一个数
*
*/
public class InsertNumInBST {
public static void main(String[] args) {
TreeNode root = new TreeNode(5);
root.left = new TreeNode(2);
root.right = new TreeNode(6);
root.left.left = new TreeNode(1);
root.right.right = new TreeNode(7);
root.left.right = new TreeNode(4);
root.left.right.left = new TreeNode(3);
TreeNode node = insert(root, 20);
System.out.println(node.val);
}
static TreeNode insert(TreeNode root,int num) {
if (root == null) return new TreeNode(num);
if (root.val == num) return root;
if (num < root.val) return insert(root.left, num);
return insert(root.right, num);
}
}
在BST中插入一个数
猜你喜欢
转载自blog.csdn.net/qq_36986015/article/details/113700849
今日推荐
周排行