diff --git a/dart/binary_tree_postorder_traversal.dart b/dart/binary_tree_postorder_traversal.dart new file mode 100644 index 0000000..a157edf --- /dev/null +++ b/dart/binary_tree_postorder_traversal.dart @@ -0,0 +1,31 @@ +/* +Given the root of a binary tree, return the postorder traversal of its nodes' values. +*/ + +/* +Definition for a binary tree node. +class TreeNode { + int val; + TreeNode? left; + TreeNode? right; + TreeNode([this.val = 0, this.left, this.right]); +} +*/ + +class Solution { + List postorderTraversal(TreeNode? root) { + final List ans = []; + postorder(root, ans); + return ans; + } + + void postorder(TreeNode? root, List ans) { + if (root == null) { + return; + } + + postorder(root.left, ans); + postorder(root.right, ans); + ans.add(root.val); + } +}