Found 10784 Articles for Python

Program to create linked list to binary search tree in Python

Arnab Chakraborty
Updated on 10-Oct-2020 11:24:02

938 Views

Suppose we have a sorted linked list node of size n, we have to create a binary search tree by Taking the value of the k = floor of (n / 2) the smallest setting it as the root. Then recursively constructing the left subtree using the linked list left of the kth node. And recursively constructing the right subtree using the linked list right of the kth node.So, if the input is like [2, 4, 5, 7, 10, 15], then the output will beTo solve this, we will follow these steps−Define a method solve(), this will take nodeif node ... Read More

Program to convert level order binary tree traversal to linked list in Python

Arnab Chakraborty
Updated on 10-Oct-2020 11:02:09

319 Views

Suppose we have a binary search tree, we have to convert it to a singly linked list using levelorder traversal.So, if the input is likethen the output will be [5, 4, 10, 2, 7, 15, ]To solve this, we will follow these steps −head := a new linked list nodecurrNode := headq := a list with value rootwhile q is not empty, docurr := delete first element from qif curr is not null, thennext of currNode := a new linked list node with value of currcurrNode := next of currNodeinsert left of curr at the end of qinsert right curr ... Read More

Program to traverse binary tree level wise in alternating way in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:54:23

93 Views

Suppose we have binary tree, we have to show the values of each level by alternating from going left-to-right and right-to-left.So, if the input is likethen the output will be [5, -10, 4, -2, -7, 15]To solve this, we will follow these steps −if root is null, thenreturn a new lists1 := a list initially insert roots2 := a new listres := a new listwhile s1 is not empty or s2 is not empty, dowhile s1 is not empty, donode := delete last element from s1if left of node is not null, theninsert left of node at the end of ... Read More

Program to find length of longest balanced subsequence in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:47:50

289 Views

Suppose we have a string s containing brackets parenthesis "(" and ")", we have to find the length of the longest subsequence of balanced brackets.So, if the input is like s = "())(()(", then the output will be 4, as we can take the subsequence like "()()"To solve this, we will follow these steps −res := 0n := size of sclose := 0for i in range n - 1 to 0, decrease by 1, doif s[i] is same as ")", thenclose := close + 1otherwise, if close > 0, thenclose := close - 1res := res + 2return resLet us ... Read More

Program to find leftmost deepest node of a tree in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:34:00

281 Views

Suppose we have a binary tree; we have to find the value of the deepest node. If there are more than one deepest node, then return the leftmost deepest node.So, if the input is likethen the output will be 4 as 4 and 7 are deepest but 4 is left most.To solve this, we will follow these steps −queue := a queue with one node root.left_max := value of rootwhile size of queue > 0, dolevel_size := size of queuefor i in range 0 to level_size, dotemp := first element from queue and delete itif i is same as 0, ... Read More

Program to check whether all leaves are at same level or not in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:28:22

123 Views

Suppose we have a binary tree; we have to check whether all leaves are at the same level or not.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs() . This will take root, dif root is not null, thenif left of root is null and right of root is null, theninsert d at the end of depthotherwise, dfs(left of root, d + 1)dfs(right of root, d + 1)From the main method, do the following −depth := a new listdfs(root, 0)return true when depth has only one valueLet ... Read More

Program to check whether we can fill square where each row and column will hold distinct elements in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:39:48

34 Views

Suppose we have one n × n matrix containing values from 0 to n. Here 0 represents an unfilled square, we have to check whether we can fill empty squares such that in each row and each column every number from 1 to n appears exactly once.So, if the input is like002201123then the output will be True, as we can set the matrix to312231123To solve this, we will follow these steps −Define a function find_empty_cell() . This will take matrix, nfor i in range 0 to n, dofor j in range 0 to n, doif matrix[i, j] is same as ... Read More

Program to find the largest sum of the path between two nodes in a binary tree in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:29:23

164 Views

Suppose we have a binary tree; we have to find the maximum sum of any path between any two nodes.So, if the input is likethen the output will be 62 as the nodes are [12, 13, 14, 16, 7].To solve this, we will follow these steps −Define a function utils() . This will take rootif root null, thenreturn 0l := utils(left of root)r := utils(right of root)max_single := maximum of (max of l and r) + value of root) and value of rootmax_top := maximum of max_single and l + r + value of rootres := maximum of res and ... Read More

Program to find largest sum of non-adjacent elements of a list in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:18:41

678 Views

Suppose we have a list of numbers called nums, we will define a function that returns the largest sum of non-adjacent numbers. Here the numbers can be 0 or negative.So, if the input is like [3, 5, 7, 3, 6], then the output will be 16, as we can take 3, 7, and 6 to get 16.To solve this, we will follow these steps−if size of nums

Program to find sum of contiguous sublist with maximum sum in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:15:29

753 Views

Suppose we have an array A. We have to find the contiguous sublist which has the maximum sum, and also return its sum. So if the array A is like A = [-2, 1, -3, 4, -1, 2, 1, -5, 4], then the sum will be 6. And the subarray will be [4, -1, 2, 1].To solve this we will try to use Dynamic programming approach.define an array dp same as the size of A, and fill it with 0dp[0] := A[0]for i := 1 to size of A – 1dp[i] := maximum of dp[i – 1] + A[i] and ... Read More

Advertisements