110. Balanced Binary Tree

Standard

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

Java Solution:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Result {
    boolean val;
    Result() {
        this.val = true;
    }
    public void setValue(boolean val) {
        this.val = val;
    }
    public boolean getValue() {
        return val;
    }
}
class Solution {
    public boolean isBalanced(TreeNode root) {
        Result isBalanced = new Result();
        getHeight(root, isBalanced);
        return isBalanced.getValue();
    }
    
    private int getHeight(TreeNode node, Result isBalanced) {
        if (node == null) return 0;
        int leftHeight = getHeight(node.left, isBalanced);
        int rightHeight = getHeight(node.right, isBalanced);
        if (Math.abs(leftHeight - rightHeight) > 1) {
            isBalanced.setValue(false);
        }
        return Math.max(leftHeight, rightHeight) + 1;
    }
}

Essentially, trying to “hack” the pass-by-reference C++ has into the Java solution. But it solves the problem in one pass.

Deploy Jupyter behind Apache reverse proxy

Standard

Jupyter was deployed on one LXD container and Apache was deployed on another. At first Jupyter kept complaining “Connecting to kernel… Failed.”

Ubuntu: 16.04LTS

Apache: Server version: Apache/2.4.18 (Ubuntu)

Solution:

Add those two lines BEFORE “/” one:

ProxyPass /api/kernels/ ws://xxx.lxd:8888/api/kernels/
ProxyPassReverse /api/kernels/ http://xxx.lxd:8888/api/kernels/

AND: make sure proxy_wstunnel is enabled for Apache:

a2enmod proxy proxy_wstunnel

Convertion of grey code and binary

Standard

Source: http://www.cnblogs.com/grandyang/p/4315607.html

以下转换代码摘自维基百科 Wikipedia:

/*
The purpose of this function is to convert an unsigned
binary number to reflected binary Gray code.

The operator >> is shift right. The operator ^ is exclusive or.
*/
unsigned int binaryToGray(unsigned int num)
{
return (num >> 1) ^ num;
}

/*
The purpose of this function is to convert a reflected binary
Gray code number to a binary number.
*/
unsigned int grayToBinary(unsigned int num)
{
unsigned int mask;
for (mask = num >> 1; mask != 0; mask = mask >> 1)
{
num = num ^ mask;
}
return num;
}

Left handed cursor for Mac

Standard

Thank bondingfortoday for this amazing method. It saves me, and it works on OS El Capitan:

Source: https://discussions.apple.com/thread/3046732

bondingfortodayApr 12, 2015 7:55 PM
Re: Left handed mouse pointerin response to sipilevi
You can use the following app called Mousecape:

https://github.com/alexzielenski/Mousecape/releases/tag/0.0.5

After downloading, installing both the app and the helper app, do the following:

  1. Go to the Mousecape preferences and choose “I am left handed”.
  2. Dump the system cursors into a cape from the menu: “Capes” → “Dump Cursors”
  3. Apply the Cursor Dump cape (right click → apply)
  4. Done!