postorder-traversal.dart (627B)
1 //Return a list of values in post order. 2 //Post order is from bottom to top and left to right. 3 //As such it would go bottom left, bottom right, root if there were three values. 4 //The time complexity of this code is O(n) where n is the number of nodes in the tree. 5 //Time: 255ms Beats: 63.16% 6 //Memory: 142.3MB Beats: 5.26% 7 8 class Solution { 9 List<int> vals = []; 10 List<int> postorderTraversal(TreeNode? root) { 11 recurse(root); 12 return vals; 13 } 14 15 void recurse(TreeNode? root){ 16 if(root == null){ 17 return; 18 } 19 recurse(root?.left); 20 recurse(root?.right); 21 vals.add(root.val); 22 } 23 }