zoukankan      html  css  js  c++  java
  • second minimum of Tournament tree。

    http://www.geeksforgeeks.org/second-minimum-element-using-minimum-comparisons/

    Given a non-empty special binary tree consisting of nodes with the non-negative value, 

    where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes,
    then this node's value is the smaller value among its two sub-nodes. Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree. If no such second minimum value exists, output -1 instead. Example 1: Input: 2 / 2 5 / 5 7 Output: 5 Explanation: The smallest value is 2, the second smallest value is 5. Example 2: Input: 2 / 2 2 Output: -1 Explanation: The smallest value is 2, but there isn't any second smallest value.

     我的思路是不断调用之前写的找第二小的(find_sec_min),当找到一个第二小的之后,把这棵树里面所有最小值改写成第二小的值,然后再调用find_sec_min,这时候找到的第二小值在原来的树里面就是第三小的值了,以此类推。当然其中还有一些判断细节要处理。。不过我觉得这样应该可以work,那个面试官好像也没什么意见。

    for left and right sub-node, if their value is the same with the parent node value, need to using recursion to find the next candidate,

    otherwise use their node value as the candidate.

    tournamenttree

    public int findSecondMinimumValue(TreeNode root) {
        if (root == null) {
            return -1;
        }
        if (root.left == null && root.right == null) {
            return -1;
        }
        
        int left = root.left.val;
        int right = root.right.val;
        
        // if value same as root value, need to find the next candidate
        if (root.left.val == root.val) {
            left = findSecondMinimumValue(root.left);
        }
        if (root.right.val == root.val) {
            right = findSecondMinimumValue(root.right);
        }
        
        if (left != -1 && right != -1) {
            return Math.min(left, right);
        } else if (left != -1) {
            return left;
        } else {
            return right;
        }
    }
    

      

    /** 
    * A tournament tree is a binary tree 
    * where the parent is the minimum of the two children. 
    * Given a tournament tree find the second minimum value in the tree. 
    * A node in the tree will always have 2 or 0 children. 
    * Also all leaves will have distinct and unique values. 
    * 2 
    * /  
    * 2 3 
    * /  |  
    * 4 2 5 3 
    * 
    * In this given tree the answer is 3. 
    */
    
    
    class Node {
      Integer value;
      Node left, right;
      Node(Integer value, Node left, Node right) {
        this.value = value;
        this.left = left;
        this.right = right;
      }
    }
    class Solution {
      /**
      * This should return the second minimum
      * int value in the given tournament tree
      */
       public static Integer secondMin(Node root) {
    
        }
    }
    class TournamentTree {
      /**
      * This should return the second minimum
      * int value in the given tournament tree
      */
       public static Integer secondMin(Node root) {
           if(root.left == null && root.right == null) {
               return Integer.MAX_VALUE;
           }
           Node node;
           int min;
           if(root.left.value == root.value) {
               node = root.left;
               min = root.right.value;
           } else {
               node = root.right;
               min = root.left.value;
           }
           return Math.min(min, secondMin(node));
        }
    }
    

      

    why we don't use a treeset? or quick find.

  • 相关阅读:
    ISAG协议中彩信支持的几种附件格式(河南电信)
    河南电信ISAG短信下行数据格式
    SQL中varchar和nvarchar有什么区别?
    通过一个很实用的例子让你学会TSQL编程的基本语法和思想
    在读取或者保存word时,程序捕获到word异常“word无法启动转换器mswrd632 wpc”
    工作基本搞定等待周五入职
    ClickOnce发布时,资源文件添加问题
    访问IIS元数据库失败
    一个随机产生中文简体字的一个类
    QQ抢车位外挂(续)
  • 原文地址:https://www.cnblogs.com/apanda009/p/7925824.html
Copyright © 2011-2022 走看看