Print all paths of the Binary Tree with maximum element in each path greater than or equal to K | A Binary Tree node ; A recursive function to print the paths whose maximum element is greater than or equal to K . ; If the current node value is greater than or equal to k , then all the subtrees following that node will get printed , flag = 1 indicates to print the required path ; If the leaf node is encountered , then the path is printed if the size of the path vector is greater than 0 ; Append the node to the path vector ; Recur left and right subtrees ; Backtracking to return the vector and print the path if the flag is 1 ; Function to initialize the variables and call the utility function to print the paths with maximum values greater than or equal to K ; Initialize flag ; ans is used to check empty condition ; Call function that print path ; If the path doesn 't exist ; Driver Code ; Constructing the following tree : 10 / \ 5 8 / \ / \ 29 2 1 98 / \ 20 50
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def findPathUtil ( root : Node , k : int , path : list , flag : int ) : NEW_LINE INDENT global ans NEW_LINE if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if root . data >= k : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT if root . left is None and root . right is None : NEW_LINE INDENT if flag : NEW_LINE INDENT ans = 1 NEW_LINE print ( " ( " , end = " " ) NEW_LINE for i in range ( len ( path ) ) : NEW_LINE INDENT print ( path [ i ] , end = " , ▁ " ) NEW_LINE DEDENT print ( root . data , end = " ) , ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT path . append ( root . data ) NEW_LINE findPathUtil ( root . left , k , path , flag ) NEW_LINE findPathUtil ( root . right , k , path , flag ) NEW_LINE path . pop ( ) NEW_LINE DEDENT def findPath ( root : Node , k : int ) : NEW_LINE INDENT global ans NEW_LINE flag = 0 NEW_LINE ans = 0 NEW_LINE v = [ ] NEW_LINE findPathUtil ( root , k , v , flag ) NEW_LINE if ans == 0 : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT ans = 0 NEW_LINE k = 25 NEW_LINE root = Node ( 10 ) NEW_LINE root . left = Node ( 5 ) NEW_LINE root . right = Node ( 8 ) NEW_LINE root . left . left = Node ( 29 ) NEW_LINE root . left . right = Node ( 2 ) NEW_LINE root . right . right = Node ( 98 ) NEW_LINE root . right . left = Node ( 1 ) NEW_LINE root . right . right . right = Node ( 50 ) NEW_LINE root . left . left . left = Node ( 20 ) NEW_LINE findPath ( root , k ) NEW_LINE DEDENT