Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions dart/binary_tree_postorder_traversal.dart
Original file line number Diff line number Diff line change
@@ -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<int> postorderTraversal(TreeNode? root) {
final List<int> ans = [];
postorder(root, ans);
return ans;
}

void postorder(TreeNode? root, List<int> ans) {
if (root == null) {
return;
}

postorder(root.left, ans);
postorder(root.right, ans);
ans.add(root.val);
}
}