LeetCode Sqrt(x)


Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

The original problem is here.

The original code is here.

My Solution

I solve this problem in C++, as below:

*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
using namespace std;

class Solution {
    int mySqrt(int x) {
        if(x <= 0)
            return -1;
        if(x == 1)
            return 1;
        int low  = 1;
        int high = x/2+1;
        int mid;
            mid = (low+high)/2;
            if(mid <= x/mid  && (mid+1) > x/(mid+1))
                return mid;
            else if(mid > x/mid)
                high -= 1;
                low += 1;
        return -1;


To solve the problem, binary search is needed.