-
Notifications
You must be signed in to change notification settings - Fork 2
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
nghoanglong
committed
Aug 14, 2021
1 parent
73de7fd
commit 57c4825
Showing
3 changed files
with
90 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Link: https://leetcode.com/problems/binary-tree-inorder-traversal/ | ||
# Site: LeetCode | ||
# Name: Binary Tree Inorder Traversal | ||
# Easy | ||
# ====================================================== | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
# 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 root is None: return [] | ||
left = self.inorderTraversal(root.left) | ||
right = self.inorderTraversal(root.right) | ||
return left + [root.val] + right |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Link: https://leetcode.com/problems/maximum-depth-of-binary-tree/ | ||
# Site: LeetCode | ||
# Name: Maximum Depth of Binary Tree | ||
# Easy | ||
# ====================================================== | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
|
||
# 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 maxDepth(self, root: Optional[TreeNode]) -> int: | ||
if root is None: return 0 | ||
else: | ||
left_height = self.maxDepth(root.left) | ||
right_height = self.maxDepth(root.right) | ||
max_height = max(left_height, right_height) | ||
return 1 + max_height | ||
|