I've been trying to switch over to Java from Node and one thing I'm wondering about is how to construct a Binary Tree without putting a sorting algorithm in. In Node, I could simply type the following:
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
let tree = new TreeNode(4);
tree.left = new TreeNode(2);
tree.left.left = new TreeNode(1);
What is the Java equivalent to this? This is my current thought process
public class BinaryTree {
private static TreeNode root;
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
bt.TreeNode = ??
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
}