就判断是否是2×平衡树,递归求解
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int is = true; int isok(TreeNode* root){ if(root == NULL) return 0; if(root -> left == NULL && root -> right == NULL) return 1; int dl = 0, dr = 0; dl = isok(root -> left) + 1; dr = isok(root -> right) + 1; if(abs(dl - dr) >= 2) is = false; return dl > dr ? dl : dr; } bool isBalanced(TreeNode* root) { isok(root); return is; }};新闻热点
疑难解答