Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ build/
*.log

get-pip.py
http_ca.crt

37 changes: 37 additions & 0 deletions Tree/cpp/112_leetcode.cpp
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions Tree/python/199_leetcode.py
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions Tree/python/operations.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Run Flake8
echo "Running Flake8..."
flake8 ./ --exclude tests,migrations
flake8 ./ --exclude tests,migrations,venv

# Run Black
echo "Running Black..."
Expand Down
Loading