commit 3e6254f9766cba11d2816c7ed68eea3b8cc01beb parent 802f21c976ae5c5624505366620ee360caaee013 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Thu, 20 Apr 2023 08:54:20 -0500 Completed binary tree traversal Diffstat:
| A | binary-tree-inorder/binary-tree-traversal.dart | | | 28 | ++++++++++++++++++++++++++++ |
1 file changed, 28 insertions(+), 0 deletions(-)
diff --git a/binary-tree-inorder/binary-tree-traversal.dart b/binary-tree-inorder/binary-tree-traversal.dart @@ -0,0 +1,28 @@ +//This is a recursive solution to print out the values +//in a binary tree inorder. The time complexity of this +//is O(n) where n is the number of nodes in the tree. +//Runtime:274ms Beats: 43.59% +//Memory: 139.9MB Beats: 69.23% + +class TreeNode { + int val; + TreeNode? left; + TreeNode? right; +} + +class Solution { + List<int> vals = []; + List<int> inorderTraversal(TreeNode? root) { + recurse(root); + return vals; + } + + void recurse(TreeNode? node){ + if(node == null){ + return; + } + recurse(node.left); + vals.add(node.val); + recurse(node.right); + } +}