Saturday, 22 September 2018

Algorithm for finding maximum element in binary tree java

public int findMax(Node node) {
        int maxVal = 0, root_val ,left_val,right_val;
       
        if(null!=node) {
            root_val = node.value;
            left_val = findMax(node.left);
            right_val = findMax(node.right);   
            maxVal = left_val>right_val ? left_val :right_val;
            maxVal = maxVal>root_val ? maxVal : root_val;
        }
       
       
        return maxVal;
    }

No comments:

Post a Comment

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...