• 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.

  • 相关阅读:
    喜讯|恒生电子LightDB完成分布式数据产品稳定性测试
    PostgreSQL limit的ties子句
    实时通信 | pusher 如何使用私有频道(四)
    实时通信 | pusher 客户端事件(五)
    实时通信 | pusher 案例:实时图表(六)
    实时通信 | pusher 演示与频道实时通信(三)
    PHP系列 | MeiliSearch 轻量搜索引擎入门介绍
    实时通信 | pusher 频道介绍(二)
    实时通信 | pusher 入门教程(一)
    袁创:总结创业十年经验
  • 原文地址:https://www.cnblogs.com/apanda009/p/7925824.html
Copyright © 2020-2023  润新知