diff --git a/.gitignore b/.gitignore index e04055d..540ec7e 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,5 @@ build/ *.log get-pip.py +http_ca.crt diff --git a/Tree/cpp/112_leetcode.cpp b/Tree/cpp/112_leetcode.cpp new file mode 100644 index 0000000..6c2bc22 --- /dev/null +++ b/Tree/cpp/112_leetcode.cpp @@ -0,0 +1,37 @@ +/* +Given the root of a binary tree and an integer targetSum, +return true if the tree has a root-to-leaf path such that adding +up all the values along the path equals targetSum. +*/ + +// Time O(N) SpaceO(N) + +//C++ Implementation +class Solution { +public: + bool hasPathSum(TreeNode* root, int targetSum) { + if(root==NULL) + return false; + + bool leftPossible = hasPathSum(root->left, targetSum - root->val); + bool rightPossible = hasPathSum(root->right, targetSum - root->val); + + if(targetSum==root->val and !root->left and !root->right) + return true; + + return leftPossible | rightPossible; + } +}; + +// Python Implementation +class Solution: + # Time Complexity O(N) Space Complexity O(N) + def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: + if root is None: + return False + + if not root.left and not root.right: + return targetSum == root.val + + targetSum -= root.val + return self.hasPathSum(root.left, targetSum) or self.hasPathSum(root.right, targetSum) \ No newline at end of file diff --git a/Tree/python/199_leetcode.py b/Tree/python/199_leetcode.py new file mode 100644 index 0000000..edd5f3e --- /dev/null +++ b/Tree/python/199_leetcode.py @@ -0,0 +1,32 @@ +""" +Given the root of a binary tree, imagine yourself +standing on the right side of it, return the values +of the nodes you can see ordered from top to bottom. +""" + +from typing import Optional, List + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + @staticmethod + def right_side_view(root: Optional[TreeNode]) -> List[int]: + def dfs(node: Optional[TreeNode], level: int): + if not node: + return + + if level == len(right_view): + right_view.append(node.val) + + dfs(node.right, level + 1) + dfs(node.left, level + 1) + + right_view = [] + dfs(root, 0) + return right_view diff --git a/Tree/python/operations.py b/Tree/python/operations.py new file mode 100644 index 0000000..d8d6228 --- /dev/null +++ b/Tree/python/operations.py @@ -0,0 +1,50 @@ +from node import Node + + +def get_height(root: 'Node') -> int: + if root is None: + return 0 + + left_ht = get_height(root.left) + right_ht = get_height(root.right) + + return max(left_ht, right_ht) + 1 + + +def get_count(root: 'Node') -> int: + if root is None: + return 0 + + left_count = get_count(root.left) + right_count = get_count(root.right) + + return left_count + right_count + 1 + + +def get_sum(root: 'Node') -> int: + if root is None: + return 0 + + left_sum = get_sum(root.left) + right_sum = get_sum(root.right) + + return root.data + left_sum + right_sum + + +def tree_diameter(root: 'Node') -> int: + def helper(root: 'Node') -> (int, int): + if root is None: + return 0, 0 + + left_height, left_diameter = helper(root.left) + right_height, right_diameter = helper(root.right) + + current_height = max(left_height, right_height) + 1 + dia_through_curr = left_height + right_height + 1 + + current_diameter = max(left_diameter, right_diameter, dia_through_curr) + + return current_height, current_diameter + + _, diameter = helper(root) + return diameter diff --git a/lint.sh b/lint.sh index 719797b..6f71bcd 100644 --- a/lint.sh +++ b/lint.sh @@ -2,7 +2,7 @@ # Run Flake8 echo "Running Flake8..." -flake8 ./ --exclude tests,migrations +flake8 ./ --exclude tests,migrations,venv # Run Black echo "Running Black..."