- 二叉排序树
思路: 二叉排序树,也就是要遵守左子树<根节点,右子树>根节点的规则。
/**
* 二叉排序树
*
* @param root
* @param data
* @return
*/
public static Tree insert(Tree root, int data) {
// 1.判空
if (root == null) {
return new Tree(data);
}
// 2.判断该值是否比父节点小,是则递归左边
Tree currentTree;
if (data < root.val) {
currentTree = insert(root.left, data);
root.left = currentTree;
} else {
// 3.该值比父节点大,递归右边
currentTree = insert(root.right, data);
root.right = currentTree;
}
return root;
}