Skip to content

Instantly share code, notes, and snippets.

@jochy
Created December 11, 2022 16:33
Show Gist options
  • Save jochy/95731bc1e73940ed5d091df4bf958614 to your computer and use it in GitHub Desktop.
Save jochy/95731bc1e73940ed5d091df4bf958614 to your computer and use it in GitHub Desktop.
dart-node-correction

dart-node-correction

Created with <3 with dartpad.dev.

void main() {
var root = Node(
value: 3,
left: Node(value: 2, left: Node(value: 1)),
right: Node(value: 5, left: Node(value: 4), right: Node(value: 10)),
);
print(root.find(3));
print(root.find(1));
print(root.find(11));
}
class Node {
final int value;
final Node? left;
final Node? right;
Node({required this.value, this.left, this.right});
Node? find(int val) {
if (value == val) return this;
if (val < value && left != null) return left!.find(val);
if (val > value && right != null) return right!.find(val);
return null;
}
String toString() => "Node with value = ${value.toString()}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment