From d8d68dd126f66649da2fddedea81b3c828947a4d Mon Sep 17 00:00:00 2001 From: ananya321 <76964451+ananya321@users.noreply.github.com> Date: Sat, 23 Oct 2021 12:11:16 +0530 Subject: [PATCH] Create binary tree inorder traversal --- binary tree inorder traversal | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 binary tree inorder traversal diff --git a/binary tree inorder traversal b/binary tree inorder traversal new file mode 100644 index 00000000..cff53921 --- /dev/null +++ b/binary tree inorder traversal @@ -0,0 +1,21 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: + inorder = [] + stack = [root] + while stack: + root = stack[-1] + if root: + stack.append(root.left) + else: + stack.pop() + if stack: + root = stack.pop() + inorder.append(root.val) + stack.append(root.right) + return inorder