• [leetcode]Balanced Binary Tree


    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        pair<bool , int> search(TreeNode* root){
            if(root == NULL) return make_pair(true , 0);
            pair<bool , int> left = search(root -> left);
            pair<bool , int> right = search(root -> right);
            if( left.first == false || right.first == false) return make_pair(false , -1);
            int depL = left.second;
            int depR = right.second;
            if(abs(depL - depR) > 1)
                return make_pair(false , -1);
            return make_pair(true , max(depL , depR) + 1);
        }
        bool isBalanced(TreeNode *root) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            pair<bool , int> ans = search(root);
            return ans.first;
        }
    };
  • 相关阅读:
    linux内存和swap
    Linux awk sort
    redis aof和rdb区别
    STL中的map、unordered_map、hash_map
    mysql 冷热备份
    redis
    linux 几个命令
    linux erase
    group by
    现在很多技术知识点缺乏来龙去脉的介绍
  • 原文地址:https://www.cnblogs.com/x1957/p/3396151.html
Copyright © 2020-2023  润新知