-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d706796
commit a3da6b5
Showing
5 changed files
with
161 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,35 @@ | ||
# Problem: Binary_Tree_Preorder_Traversal | ||
# URL: https://leetcode.com/problems/binary-tree-preorder-traversal/description/ | ||
|
||
def solution(): | ||
# TODO: Implement the solution for Binary_Tree_Preorder_Traversal | ||
pass | ||
# 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 preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: | ||
# ele=[] | ||
# if root: | ||
# stack=[] | ||
# stack.append(root) | ||
# while len(stack)>0: | ||
# popped=stack.pop() | ||
# ele.append(popped.val) | ||
# if popped.right: | ||
# stack.append(popped.right) | ||
# if popped.left: | ||
# stack.append(popped.left) | ||
# return ele | ||
|
||
## Recurssive approach | ||
def preOrder(node,ele): | ||
if node: | ||
ele.append(node.val) | ||
preOrder(node.left,ele) | ||
preOrder(node.right,ele) | ||
ele=[] | ||
preOrder(root,ele) | ||
return ele | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,40 @@ | ||
# Problem: Binary_Tree_Inorder_Traversal | ||
# URL: https://leetcode.com/problems/binary-tree-inorder-traversal/description/ | ||
|
||
def solution(): | ||
# TODO: Implement the solution for Binary_Tree_Inorder_Traversal | ||
pass | ||
# 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]: | ||
# if not root: | ||
# return | ||
# def inOrder(root,ele): | ||
# if root.left: | ||
# inOrder(root.left,ele) | ||
# ele.append(root.val) | ||
# if root.right: | ||
# inOrder(root.right,ele) | ||
|
||
# ele=[] | ||
# inOrder(root,ele) | ||
# return ele; | ||
|
||
## iterative approach | ||
if not root: | ||
return | ||
traversed=[] | ||
stack=[] | ||
curr=root | ||
while stack or curr: | ||
if curr: | ||
stack.append(curr) | ||
curr=curr.left | ||
elif len(stack)>0: | ||
curr=stack.pop() | ||
traversed.append(curr.val) | ||
curr=curr.right | ||
return traversed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,42 @@ | ||
# Problem: Binary_Tree_Postorder_Traversal | ||
# URL: https://leetcode.com/problems/binary-tree-postorder-traversal/description/ | ||
|
||
def solution(): | ||
# TODO: Implement the solution for Binary_Tree_Postorder_Traversal | ||
pass | ||
# 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 postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: | ||
# if not root: | ||
# return | ||
# def postOrder(root,ele): | ||
# if root.left: | ||
# postOrder(root.left,ele) | ||
# if root.right: | ||
# postOrder(root.right,ele) | ||
# ele.append(root.val) | ||
# ele=[] | ||
# postOrder(root,ele) | ||
# return ele | ||
|
||
## iterative approach | ||
if not root: | ||
return | ||
stack1=[] | ||
stack2=[] | ||
current=root | ||
stack1.append(current) | ||
while stack1: | ||
current=stack1.pop() | ||
stack2.append(current.val) | ||
if current.left: | ||
stack1.append(current.left) | ||
if current.right: | ||
stack1.append(current.right) | ||
traversed=[] | ||
while stack2: | ||
data=stack2.pop() | ||
traversed.append(data) | ||
return traversed |