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.

  • 相关阅读:
    SSM框架整合(Spring+SrpingMVC+Mybatis) 简单案例
    SpringDataRedis操作Redis简单案例
    SpringMVC总结四:拦截器简单介绍
    Configure a bridge interface over a VLAN tagged bonded interface
    Create a bridge using a tagged vlan (8021.q) interface
    Configure a VLAN (on top of a bond) with NetworkManager (nmcli) in RHEL7
    Configure bridge on a team interface using NetworkManager in RHEL 7
    Configure a bridged network interface for KVM using RHEL 5.4 or later?
    程序员的成长阶梯和级别定义
    <程序员从入门到精通> -- How
  • 原文地址:https://www.cnblogs.com/apanda009/p/7925824.html
Copyright © 2011-2022 走看看