commit b9512011fe817ddb7061d8446411bdeecbdbd0b4 parent d5d4fad717c416c1586c24d3b277d72518ca8924 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Wed, 10 May 2023 21:07:04 -0500 Completed postorder traversal problem using dart Diffstat:
| A | binary-tree-postorder-traversal/postorder-traversal.dart | | | 23 | +++++++++++++++++++++++ |
1 file changed, 23 insertions(+), 0 deletions(-)
diff --git a/binary-tree-postorder-traversal/postorder-traversal.dart b/binary-tree-postorder-traversal/postorder-traversal.dart @@ -0,0 +1,23 @@ +//Return a list of values in post order. +//Post order is from bottom to top and left to right. +//As such it would go bottom left, bottom right, root if there were three values. +//The time complexity of this code is O(n) where n is the number of nodes in the tree. +//Time: 255ms Beats: 63.16% +//Memory: 142.3MB Beats: 5.26% + +class Solution { + List<int> vals = []; + List<int> postorderTraversal(TreeNode? root) { + recurse(root); + return vals; + } + + void recurse(TreeNode? root){ + if(root == null){ + return; + } + recurse(root?.left); + recurse(root?.right); + vals.add(root.val); + } +}