Skip to content
Open
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions InterviewBit/MAXDepthofBinaryTree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
# @param A : root node of tree
# @return an integer
def maxDepth(self, A):
if A is None:
return 0
return max(self.maxDepth(A.left),self.maxDepth(A.right))+1
24 changes: 24 additions & 0 deletions InterviewBit/MINDepthofBinaryTree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
# @param A : root node of tree
# @return an integer
def minDepth(self, A):
if A is None:
return 0


if A.left==None and A.right==None:
return 1

if A.left==None:
return self.minDepth(A.right)+1
if A.right==None:
return self.minDepth(A.left)+1
return 1 + min(self.minDepth(A.left),self.minDepth(A.right))

18 changes: 18 additions & 0 deletions InterviewBit/Roottoleafpath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
# @param A : root node of tree
# @param B : integer
# @return a list of list of integers
def pathSum(self, A, B):
if A is None:
return []
q=[[A,A.val,[A.val]]]
c=[]
while q:
a=q.pop(0)
if a[0].left==None and a[0].right==None and a[1]==B:
c.append(a[2])
if a[0].left:
q.append([a[0].left,a[1]+a[0].left.val,a[2]+[a[0].left.val]])
if a[0].right:
q.append([a[0].right,a[1]+a[0].right.val,a[2]+[a[0].right.val]])
return c
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.