codeparrot/xlcost-text-to-code huggingface dataset:
Insertion at Specific Position in a Circular Doubly Linked List | Doubly linked list node ; Utility function to create a node in memory ; Function to display the list ; Function to count nunmber of elements in the list ; Declare temp pointer to traverse the list ; Variable to store the count ; Iterate the list and increment the count ; As the list is circular , increment the counter at last ; Function to insert a node at a given position in the circular doubly linked list ; Declare two pointers ; Create a new node in memory ; Point temp to start ; count of total elements in the list ; If list is empty or the position is not valid , return false ; Assign the data ; Iterate till the loc ; See in Image , circle 1 ; See in Image , Circle 2 ; See in Image , Circle 3 ; See in Image , Circle 4 ; Function to create circular doubly linked list from array elements ; Declare newNode and temporary pointer ; Iterate the loop until array length ; Create new node ; Assign the array data ; If it is first element Put that node prev and next as start as it is circular ; Find the last node ; Add the last node to make them in circular fashion ; Array elements to create circular doubly linked list ; Start Pointer ; Create the List ; Display the list before insertion ; Inserting 8 at 3 rd position ; Display the list after insertion
class node { constructor ( ) { this . data = 0 ; this . next = null ; this . prev = null ; } } function getNode ( ) { return new node ( ) ; } function displayList ( temp ) { var t = temp ; if ( temp == null ) return 0 ; else { document . write ( " " ) ; while ( temp . next != t ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( temp . data + " " ) ; return 1 ; } } function countList ( start ) { var temp = start ; var count = 0 ; while ( temp . next != start ) { temp = temp . next ; count ++ ; } count ++ ; return count ; } function insertAtLocation ( start , data , loc ) { var temp , newNode ; var i , count ; newNode = getNode ( ) ; temp = start ; count = countList ( start ) ; if ( temp == null count < loc ) return start ; else { newNode . data = data ; for ( i = 1 ; i < loc - 1 ; i ++ ) { temp = temp . next ; } newNode . next = temp . next ; temp . next . prev = newNode ; temp . next = newNode ; newNode . prev = temp ; return start ; } } function createList ( arr , n , start ) { var newNode , temp ; var i ; for ( i = 0 ; i < n ; i ++ ) { newNode = getNode ( ) ; newNode . data = arr [ i ] ; if ( i == 0 ) { start = newNode ; newNode . prev = start ; newNode . next = start ; } else { temp = start . prev ; temp . next = newNode ; newNode . next = start ; newNode . prev = temp ; temp = start ; temp . prev = newNode ; } } return start ; } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var n = arr . length ; var start = null ; start = createList ( arr , n , start ) ; displayList ( start ) ; start = insertAtLocation ( start , 8 , 3 ) ; displayList ( start ) ;
Convert an Array to a Circular Doubly Linked List | Doubly linked list node ; Utility function to create a node in memory ; Function to display the list ; Function to convert array into list ; Declare newNode and temporary pointer ; Iterate the loop until array length ; Create new node ; Assign the array data ; If it is first element Put that node prev and next as start as it is circular ; Find the last node ; Add the last node to make them in circular fashion ; Array to be converted ; Start Pointer ; Create the List ; Display the list
class node { constructor ( ) { this . data = 0 ; this . next = this . prev = null ; } } function getNode ( ) { return new node ( ) ; } function displayList ( temp ) { let t = temp ; if ( temp == null ) return 0 ; else { document . write ( " " ) ; while ( temp . next != t ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( temp . data ) ; return 1 ; } } function createList ( arr , n , start ) { let newNode , temp ; let i ; for ( i = 0 ; i < n ; i ++ ) { newNode = getNode ( ) ; newNode . data = arr [ i ] ; if ( i == 0 ) { start = newNode ; newNode . prev = start ; newNode . next = start ; } else { temp = ( start ) . prev ; temp . next = newNode ; newNode . next = start ; newNode . prev = temp ; temp = start ; temp . prev = newNode ; } } return start ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; let start = null ; start = createList ( arr , n , start ) ; displayList ( start ) ;
Lucky alive person in a circle | Code Solution to sword puzzle | Node structure ; Function to find the luckiest person ; Create a single node circular linked list . ; Starting from first soldier . ; Condition for evaluating the existence of single soldier who is not killed . ; Deleting soldier from the circular list who is killed in the fight . ; Returning the Luckiest soldier who remains alive . ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function newNode ( data ) { var node = new Node ( ) ; node . data = data ; node . next = null ; return node ; } function alivesol ( Num ) { if ( Num == 1 ) return 1 ; var last = newNode ( 1 ) ; last . next = last ; for ( var i = 2 ; i <= Num ; i ++ ) { var tem = newNode ( i ) ; tem . next = last . next ; last . next = tem ; last = tem ; } var curr = last . next ; var tem1 = new Node ( ) ; while ( curr . next != curr ) { tem1 = curr ; curr = curr . next ; tem1 . next = curr . next ; tem1 = tem1 . next ; curr = tem1 ; } var res = tem1 . data ; return res ; } var N = 100 ; document . write ( alivesol ( N ) + " " ) ;
Lowest Common Ancestor of the deepest leaves of a Binary Tree | Node of a Binary Tree ; Function to find the depth of the Binary Tree ; If root is not null ; Left recursive subtree ; Right recursive subtree ; Returns the maximum depth ; Function to perform the depth first search on the binary tree ; If root is null ; If curr is equal to depth ; Left recursive subtree ; Right recursive subtree ; If left and right are not null ; Return left , if left is not null Otherwise return right ; Function to find the LCA of the deepest nodes of the binary tree ; If root is null ; Stores the deepest depth of the binary tree ; Return the LCA of the nodes at level depth ; Given Binary Tree
class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function findDepth ( root ) { if ( root == null ) return 0 ; let left = findDepth ( root . left ) ; let right = findDepth ( root . right ) ; return 1 + Math . max ( left , right ) ; } function DFS ( root , curr , depth ) { if ( root == null ) return null ; if ( curr == depth ) return root ; let left = DFS ( root . left , curr + 1 , depth ) ; let right = DFS ( root . right , curr + 1 , depth ) ; if ( left != null && right != null ) return root ; return ( left != null ) ? left : right ; } function lcaOfDeepestLeaves ( root ) { if ( root == null ) return null ; let depth = findDepth ( root ) - 1 ; return DFS ( root , 0 , depth ) ; } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . right . left . left = new Node ( 8 ) ; root . right . left . right = new Node ( 9 ) ; document . write ( lcaOfDeepestLeaves ( root ) . data ) ;
Check if two nodes are on same path in a tree | Set 2 | Function to filter the return Values ; Utility function to check if nodes are on same path or not ; Condition to check if any vertex is equal to given two vertex or not ; Check if the current position has 1 ; Recursive call ; Return LCA ; Function to check if nodes lies on same path or not ; Driver code
function filter ( x , y , z ) { if ( x != - 1 && y != - 1 ) { return z ; } return x == - 1 ? y : x ; } function samePathUtil ( mtrx , vrtx , v1 , v2 , i ) { let ans = - 1 ; if ( i == v1 i == v2 ) return i ; for ( let j = 0 ; j < vrtx ; j ++ ) { if ( mtrx [ i ] [ j ] == 1 ) { ans = filter ( ans , samePathUtil ( mtrx , vrtx , v1 , v2 , j ) , i ) ; } } return ans ; } function isVertexAtSamePath ( mtrx , vrtx , v1 , v2 , i ) { let lca = samePathUtil ( mtrx , vrtx , v1 - 1 , v2 - 1 , i ) ; if ( lca == v1 - 1 lca == v2 - 1 ) return true ; return false ; } let vrtx = 7 ; let mtrx = [ [ 0 , 1 , 1 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ] ; let v1 = 1 , v2 = 5 ; if ( isVertexAtSamePath ( mtrx , vrtx , v1 , v2 , 0 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find distance between two nodes in the given Binary tree for Q queries | Javascript program to find distance between two nodes using LCA ; log2 ( MAX ) ; Array to store the level of each node ; Vector to store tree ; Pre - Processing to calculate values of lca [ ] [ ] , dist [ ] [ ] ; Using recursion formula to calculate the values of lca [ ] [ ] ; Storing the level of each node ; Using recursion formula to calculate the values of lca [ ] [ ] and dist [ ] [ ] ; Function to find the distance between given nodes u and v ; The node which is present farthest from the root node is taken as v . If u is farther from root node then swap the two ; Finding the ancestor of v which is at same level as u ; Adding distance of node v till its 2 ^ i - th ancestor ; If u is the ancestor of v then u is the LCA of u and v ; Finding the node closest to the root which is not the common ancestor of u and v i . e . a node x such that x is not the common ancestor of u and v but lca [ x ] [ 0 ] is ; Adding the distance of v and u to its 2 ^ i - th ancestor ; Adding the distance of u and v to its first ancestor ; Number of nodes ; Add edges with their cost ; Initialising lca and dist values with - 1 and 0 respectively ; Perform DFS ; Query 1 : { 1 , 3 } ; Query 2 : { 2 , 3 } ; Query 3 : { 3 , 5 }
let MAX = 1000 ; let log = 10 ; let level = new Array ( MAX ) ; let lca = new Array ( MAX ) ; let dist = new Array ( MAX ) ; let graph = [ ] ; function addEdge ( u , v , cost ) { graph [ u ] . push ( [ v , cost ] ) ; graph [ v ] . push ( [ u , cost ] ) ; } function dfs ( node , parent , h , cost ) { lca [ node ] [ 0 ] = parent ; level [ node ] = h ; if ( parent != - 1 ) { dist [ node ] [ 0 ] = cost ; } for ( let i = 1 ; i < log ; i ++ ) { if ( lca [ node ] [ i - 1 ] != - 1 ) { lca [ node ] [ i ] = lca [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; dist [ node ] [ i ] = dist [ node ] [ i - 1 ] + dist [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; } } for ( let i = 0 ; i < graph [ node ] . length ; i ++ ) { if ( graph [ node ] [ i ] [ 0 ] == parent ) continue ; dfs ( graph [ node ] [ i ] [ 0 ] , node , h + 1 , graph [ node ] [ i ] [ 1 ] ) ; } } function findDistance ( u , v ) { let ans = 0 ; if ( level [ u ] > level [ v ] ) { let temp = u ; u = v ; v = temp ; } for ( let i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v ] [ i ] != - 1 && level [ lca [ v ] [ i ] ] >= level [ u ] ) { ans += dist [ v ] [ i ] ; v = lca [ v ] [ i ] ; } } if ( v == u ) { document . write ( ans + " " ) ; } else { for ( let i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v ] [ i ] != lca [ u ] [ i ] ) { ans += dist [ u ] [ i ] + dist [ v ] [ i ] ; v = lca [ v ] [ i ] ; u = lca [ u ] [ i ] ; } } ans += dist [ u ] [ 0 ] + dist [ v ] [ 0 ] ; document . write ( ans + " " ) ; } } let n = 5 ; for ( let i = 0 ; i < MAX ; i ++ ) { graph . push ( [ ] ) ; } addEdge ( 1 , 2 , 2 ) ; addEdge ( 1 , 3 , 3 ) ; addEdge ( 2 , 4 , 5 ) ; addEdge ( 2 , 5 , 7 ) ; for ( let i = 1 ; i <= n ; i ++ ) { lca [ i ] = new Array ( log ) ; dist [ i ] = new Array ( log ) ; for ( let j = 0 ; j < log ; j ++ ) { lca [ i ] [ j ] = - 1 ; dist [ i ] [ j ] = 0 ; } } dfs ( 1 , - 1 , 0 , 0 ) ; findDistance ( 1 , 3 ) ; findDistance ( 2 , 3 ) ; findDistance ( 3 , 5 ) ;
Count of all prime weight nodes between given nodes in the given Tree | Javascript program Count prime weight nodes between two nodes in the given tree ; Function to perform Sieve Of Eratosthenes for prime number ; Initialize all entries of prime it as true a value in prime [ i ] will finally be false if i is Not a prime , else true . ; Check if prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to perform dfs ; Stores parent of each node ; Stores level of each node from root ; Function to perform prime number between the path ; The node which is present farthest from the root node is taken as v If u is farther from root node then swap the two ; Find the ancestor of v which is at same level as u ; If Weight is prime increment count ; If u is the ancestor of v then u is the LCA of u and v Now check if weigh [ v ] is prime or not ; When v and u are on the same level but are in different subtree . Now move both u and v up by 1 till they are not same ; If weight of first ancestor is prime ; Precompute all the prime numbers till MAX ; Weights of the node ; Edges of the tree
var MAX = 1000 ; var weight = Array ( MAX ) ; var level = Array ( MAX ) ; var par = Array ( MAX ) ; var prime = Array ( MAX + 1 ) . fill ( true ) ; var graph = Array . from ( Array ( MAX ) , ( ) => Array ( ) ) ; function SieveOfEratosthenes ( ) { for ( var p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * p ; i <= MAX ; i += p ) prime [ i ] = false ; } } } function dfs ( node , parent , h ) { par [ node ] = parent ; level [ node ] = h ; graph [ node ] . forEach ( child => { if ( child != parent ) dfs ( child , node , h + 1 ) ; } ) ; } function findPrimeOnPath ( u , v ) { var count = 0 ; if ( level [ u ] > level [ v ] ) { [ u , v ] = [ v , u ] } var d = level [ v ] - level [ u ] ; while ( d -- ) { if ( prime [ weight [ v ] ] ) count ++ ; v = par [ v ] ; } if ( v == u ) { if ( prime [ weight [ v ] ] ) count ++ ; return count ; } while ( v != u ) { if ( prime [ weight [ v ] ] ) count ++ ; if ( prime [ weight [ u ] ] ) count ++ ; u = par [ u ] ; v = par [ v ] ; } if ( prime [ weight [ v ] ] ) count ++ ; return count ; } SieveOfEratosthenes ( ) ; weight [ 1 ] = 5 ; weight [ 2 ] = 10 ; weight [ 3 ] = 11 ; weight [ 4 ] = 8 ; weight [ 5 ] = 6 ; graph [ 1 ] . push ( 2 ) ; graph [ 2 ] . push ( 3 ) ; graph [ 2 ] . push ( 4 ) ; graph [ 1 ] . push ( 5 ) ; dfs ( 1 , - 1 , 0 ) ; var u = 3 , v = 5 ; document . write ( findPrimeOnPath ( u , v ) ) ;
Lowest Common Ancestor for a Set of Nodes in a Rooted Tree | Set time 1 initially ; Case for root node ; In - time for node ; Out - time for the node ; level [ i ] - . Level of node i ; t_in [ i ] - . In - time of node i ; t_out [ i ] - . Out - time of node i ; Fill the level , in - time and out - time of all nodes ; To find minimum in - time among all nodes in LCA set ; To find maximum in - time among all nodes in LCA set ; Node with same minimum and maximum out time is LCA for the set ; Take the minimum level as level of LCA ; If i - th node is at a higher level than that of the minimum among the nodes of the given set ; Compare in - time , out - time and level of i - th node to the respective extremes among all nodes of the given set ; Driver code
let T = 1 ; function dfs ( node , parent , g , level , t_in , t_out ) { if ( parent == - 1 ) { level [ node ] = 1 ; } else { level [ node ] = level [ parent ] + 1 ; } t_in [ node ] = T ; for ( let i = 0 ; i < g [ node ] . length ; i ++ ) { if ( g [ node ] [ i ] != parent ) { T ++ ; dfs ( g [ node ] [ i ] , node , g , level , t_in , t_out ) ; } } T ++ ; t_out [ node ] = T ; } function findLCA ( n , g , v ) { let level = new Array ( n + 1 ) ; let t_in = new Array ( n + 1 ) ; let t_out = new Array ( n + 1 ) ; dfs ( 1 , - 1 , g , level , t_in , t_out ) ; let mint = Number . MAX_VALUE , maxt = Number . MIN_VALUE ; let minv = - 1 , maxv = - 1 ; for ( let i = 0 ; i < v . length ; i ++ ) { if ( t_in [ v [ i ] ] < mint ) { mint = t_in [ v [ i ] ] ; minv = v [ i ] ; } if ( t_out [ v [ i ] ] > maxt ) { maxt = t_out [ v [ i ] ] ; maxv = v [ i ] ; } } if ( minv == maxv ) { return minv ; } let lev = Math . min ( level [ minv ] , level [ maxv ] ) ; let node = 0 , l = Number . MIN_VALUE ; for ( let i = 1 ; i <= n ; i ++ ) { if ( level [ i ] > lev ) continue ; if ( t_in [ i ] <= mint && t_out [ i ] >= maxt && level [ i ] > l ) { node = i ; l = level [ i ] ; } } return node ; } let n = 10 ; let g = new Array ( n + 1 ) ; for ( let i = 0 ; i < g . length ; i ++ ) g [ i ] = [ ] ; g [ 1 ] . push ( 2 ) ; g [ 2 ] . push ( 1 ) ; g [ 1 ] . push ( 3 ) ; g [ 3 ] . push ( 1 ) ; g [ 1 ] . push ( 4 ) ; g [ 4 ] . push ( 1 ) ; g [ 2 ] . push ( 5 ) ; g [ 5 ] . push ( 2 ) ; g [ 2 ] . push ( 6 ) ; g [ 6 ] . push ( 2 ) ; g [ 3 ] . push ( 7 ) ; g [ 7 ] . push ( 3 ) ; g [ 4 ] . push ( 10 ) ; g [ 10 ] . push ( 4 ) ; g [ 8 ] . push ( 7 ) ; g [ 7 ] . push ( 8 ) ; g [ 9 ] . push ( 7 ) ; g [ 7 ] . push ( 9 ) ; let v = [ ] ; v . push ( 7 ) ; v . push ( 3 ) ; v . push ( 8 ) ; document . write ( findLCA ( n , g , v ) + " " ) ;
Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree | Structure of binary tree ; Function to create a new node ; Function to store the path from root node to given node of the tree in path vector and then returns true if the path exists otherwise false ; Function to print the minimum and the maximum value present in the path connecting the given two nodes of the given binary tree ; To store the path from the root node to a ; To store the path from the root node to b ; To store the minimum and the maximum value in the path from LCA to a ; To store the minimum and the maximum value in the path from LCA to b ; If both a and b are present in the tree ; Compare the paths to get the first different value ; Find minimum and maximum value in the path from LCA to a ; Find minimum and maximum value in the path from LCA to b ; Minimum of min values in first path and second path ; Maximum of max values in first path and second path ; If no path exists ; Driver Code
class Node { constructor ( key ) { this . left = null ; this . right = null ; this . data = key ; } } function newNode ( key ) { let node = new Node ( key ) ; return node ; } let path = [ ] ; function FindPath ( root , key ) { if ( root == null ) return false ; path . push ( root . data ) ; if ( root . data == key ) return true ; if ( FindPath ( root . left , key ) || FindPath ( root . right , key ) ) return true ; path . pop ( ) ; return false ; } function minMaxNodeInPath ( root , a , b ) { path = [ ] ; let flag = true ; let Path2 = [ ] , Path1 = [ ] ; let min1 = Number . MAX_VALUE ; let max1 = Number . MIN_VALUE ; let min2 = Number . MAX_VALUE ; let max2 = Number . MIN_VALUE ; let i = 0 ; let j = 0 ; flag = FindPath ( root , a ) ; Path1 = path ; path = [ ] ; flag &= FindPath ( root , b ) ; Path2 = path ; if ( flag ) { for ( i = 0 ; i < Path1 . length && i < Path2 . length ; i ++ ) if ( Path1 [ i ] != Path2 [ i ] ) break ; i -- ; j = i ; for ( ; i < Path1 . length ; i ++ ) { if ( min1 > Path1 [ i ] ) min1 = Path1 [ i ] ; if ( max1 < Path1 [ i ] ) max1 = Path1 [ i ] ; } for ( ; j < Path2 . length ; j ++ ) { if ( min2 > Path2 [ j ] ) min2 = Path2 [ j ] ; if ( max2 < Path2 [ j ] ) max2 = Path2 [ j ] ; } document . write ( " " + Math . min ( min1 , min2 ) + " " ) ; document . write ( " " + Math . max ( max1 , max2 ) + " " ) ; } else document . write ( " " + " " ) ; return 0 ; } let root = newNode ( 20 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 22 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 25 ) ; root . left . right . left = newNode ( 10 ) ; root . left . right . right = newNode ( 14 ) ; let a = 5 ; let b = 14 ; minMaxNodeInPath ( root , a , b ) ;
Sum of all odd nodes in the path connecting two given nodes | Binary Tree node ; Utitlity function to create a new Binary Tree node ; Function to check if there is a path from root to the given node . It also populates ' arr ' with the given path ; if root is null there is no path ; push the node ' s ▁ value ▁ in ▁ ' arr ' ; if it is the required node return true ; else check whether the required node lies in the left subtree or right subtree of the current node ; required node does not lie either in the left or right subtree of the current node Thus , remove current node ' s ▁ value ▁ from ▁ ▁ ' arr 'and then return false ; Function to get the sum of odd nodes in the path between any two nodes in a binary tree ; vector to store the path of first node n1 from root ; vector to store the path of second node n2 from root ; Get intersection point ; Keep moving forward until no intersection is found ; calculate sum of ODD nodes from the path ; Driver Code
class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function newNode ( data ) { let node = new Node ( data ) ; return node ; } function getPath ( root , arr , x ) { if ( root == null ) return false ; arr . push ( root . data ) ; if ( root . data == x ) return true ; if ( getPath ( root . left , arr , x ) || getPath ( root . right , arr , x ) ) return true ; arr . pop ( ) ; return false ; } function sumOddNodes ( root , n1 , n2 ) { let path1 = [ ] ; let path2 = [ ] ; getPath ( root , path1 , n1 ) ; getPath ( root , path2 , n2 ) ; let intersection = - 1 ; let i = 0 , j = 0 ; while ( i != path1 . length j != path2 . length ) { if ( i == j && path1 [ i ] == path2 [ j ] ) { i ++ ; j ++ ; } else { intersection = j - 1 ; break ; } } let sum = 0 ; for ( i = path1 . length - 1 ; i > intersection ; i -- ) if ( path1 [ i ] % 2 != 0 ) sum += path1 [ i ] ; for ( i = intersection ; i < path2 . length ; i ++ ) if ( path2 [ i ] % 2 != 0 ) sum += path2 [ i ] ; return sum ; } let root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; let node1 = 5 ; let node2 = 6 ; document . write ( sumOddNodes ( root , node1 , node2 ) ) ;
Lowest Common Ancestor in Parent Array Representation | Maximum value in a node ; Function to find the Lowest common ancestor ; Create a visited vector and mark all nodes as not visited . ; Moving from n1 node till root and mark every accessed node as visited ; Move to the parent of node n1 ; For second node finding the first node common ; Insert function for Binary tree ; Maximum capacity of binary tree ; Root marked
const MAX = 1000 ; function findLCA ( n1 , n2 , parent ) { let visited = new Array ( MAX ) . fill ( false ) ; visited [ n1 ] = true ; while ( parent [ n1 ] != - 1 ) { visited [ n1 ] = true ; n1 = parent [ n1 ] ; } visited [ n1 ] = true ; while ( ! visited [ n2 ] ) n2 = parent [ n2 ] ; return n2 ; } function insertAdj ( parent , i , j ) { parent [ i ] = j ; } let parent = new Array ( MAX ) ; parent [ 20 ] = - 1 ; insertAdj ( parent , 8 , 20 ) ; insertAdj ( parent , 22 , 20 ) ; insertAdj ( parent , 4 , 8 ) ; insertAdj ( parent , 12 , 8 ) ; insertAdj ( parent , 10 , 12 ) ; insertAdj ( parent , 14 , 12 ) ; document . write ( findLCA ( 10 , 14 , parent ) ) ;