1. 程式人生 > 遊戲 >網友分享65歲老爸打《守望先鋒》模樣 獲15萬點贊

網友分享65歲老爸打《守望先鋒》模樣 獲15萬點贊

231. 2的冪

難度簡單

給定一個整數,編寫一個函式來判斷它是否是 2 的冪次方。

示例1:

輸入: 1
輸出: true
解釋: 20= 1

示例 2:

輸入: 16
輸出: true
解釋: 24= 16

示例 3:

輸入: 218
輸出: false
class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n < 1) return false;
        if(n == 1) return true;
        
        int i = 0, power = 1;
        
        // Calculate n^i,if out of range then exit
        while(power < n){
            // To prevent power *= out of int range
            if (power > INT_MAX/2) return false;
            power *= 2;
            if(power == n) return true;
        }
        return false;
    }
};

235. 二叉搜尋樹的最近公共祖先

難度簡單

給定一個二叉搜尋樹, 找到該樹中兩個指定節點的最近公共祖先。

百度百科中最近公共祖先的定義為:“對於有根樹 T 的兩個結點 p、q,最近公共祖先表示為一個結點 x,滿足 x 是 p、q 的祖先且 x 的深度儘可能大(一個節點也可以是它自己的祖先)。”

例如,給定如下二叉搜尋樹: root =[6,2,8,0,4,7,9,null,null,3,5]

示例 1:

輸入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
輸出: 6 
解釋: 節點 2 和節點 8 的最近公共祖先是 6。

示例 2:

輸入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
輸出: 2
解釋: 節點 2 和節點 4 的最近公共祖先是 2, 因為根據定義最近公共祖先節點可以為節點本身。

說明:

  • 所有節點的值都是唯一的。
  • p、q 為不同節點且均存在於給定的二叉搜尋樹中。
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if((root==p)||(root==q))return root;
        if(root==nullptr)return root;
        if((root->left==nullptr)&&(root->right==nullptr))return nullptr;
        TreeNode *al=lowestCommonAncestor(root->left,p,q),*ar=lowestCommonAncestor(root->right,p,q);
        if((al!=nullptr)&&(ar!=nullptr))return root;
        if(al==nullptr)return ar;
        return al;
    }
};

236. 二叉樹的最近公共祖先

難度中等

給定一個二叉樹, 找到該樹中兩個指定節點的最近公共祖先。

百度百科中最近公共祖先的定義為:“對於有根樹 T 的兩個結點 p、q,最近公共祖先表示為一個結點 x,滿足 x 是 p、q 的祖先且 x 的深度儘可能大(一個節點也可以是它自己的祖先)。”

例如,給定如下二叉樹: root =[3,5,1,6,2,0,8,null,null,7,4]

示例 1:

輸入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
輸出: 3
解釋: 節點 5 和節點 1 的最近公共祖先是節點 3。

示例2:

輸入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
輸出: 5
解釋: 節點 5 和節點 4 的最近公共祖先是節點 5。因為根據定義最近公共祖先節點可以為節點本身。

說明:

  • 所有節點的值都是唯一的。
  • p、q 為不同節點且均存在於給定的二叉樹中。
class Solution {
public:
    bool nodeExistInRoot(TreeNode *root, TreeNode *node) {
        if (root == nullptr) {
            return false;
        }
        if (root == node) {
            return true;
        } else {
            return nodeExistInRoot(root -> left, node) || nodeExistInRoot(root -> right, node);
        }
    }
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        stack<TreeNode *> roots;
        roots.push(root);
        queue<TreeNode *> queue;
        queue.push(root);
        while (roots.size()) {
            TreeNode *r = roots.top();
            roots.pop();
            if (nodeExistInRoot(r -> left, p) && nodeExistInRoot(r -> left, q)) {
                roots.push(r -> left);
                queue.push(r -> left);
            }
            if (nodeExistInRoot(r -> right, p) && nodeExistInRoot(r -> right, q)) {
                roots.push(r -> right);
                queue.push(r -> right);
            }
        }
        return queue.back();
    }
};