Created with <3 with dartpad.dev.
Created
December 11, 2022 16:33
-
-
Save jochy/95731bc1e73940ed5d091df4bf958614 to your computer and use it in GitHub Desktop.
dart-node-correction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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