#100 Same Tree
#101 Symmetric Tree
#104 Maximum Depth of Binary Tree
#108 Convert Sorted Array to Binary Search Tree
#110 Balanced Binary Tree
#100 Same Tree
要求:给定2个树,判断是否完全相同
思路1:递归判别即可,上代码
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(!p && !q) return true;
if((p&&!q) || (!p&&q)) return false;
return (p->val == q->val) && isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
};
#101 Symmetric Tree
要求:给定一个树,判断是否为对称树
思路1:逐步判断左右节点的值即可
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
return isSymmetric(root->left,root->right);
}
bool isSymmetric(TreeNode* root1, TreeNode* root2) {
if(!root1 && !root2) return true;
else if((root1 &&!root2)||(!root1&&root2)||(root1->val!=root2->val)) return false;
else return isSymmetric(root1->left,root2->right) && isSymmetric(root1->right,root2->left);
}
};
参考大神博客园:Grandyang
思路2:采用队列结构,也可以搞定,确实想不到
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (!root) return true;
queue<TreeNode*> q1, q2;
q1.push(root->left);
q2.push(root->right);
while (!q1.empty() && !q2.empty()) {
TreeNode *node1 = q1.front(); q1.pop();
TreeNode *node2 = q2.front(); q2.pop();
if (!node1 && !node2) continue;
if((node1 && !node2) || (!node1 && node2)) return false;
if (node1->val != node2->val) return false;
q1.push(node1->left);
q1.push(node1->right);
q2.push(node2->right);
q2.push(node2->left);
}
return true;
}
};
#104 Maximum Depth of Binary Tree
要求:给定一个树,求最大深度
思路1:求左右子树的最大深度,如果存在就加1,即可
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root) return 0;
return 1 + max(maxDepth(root->left),maxDepth(root->right));
}
};
思路2:采用队列结构,放入根节点,只要队列不为空,深度就需要加1
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) return 0;
int res = 0;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
++res;
for (int i = q.size(); i > 0; --i) {
TreeNode *t = q.front(); q.pop();
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
}
return res;
}
};
#108 Convert Sorted Array to Binary Search Tree
要求:给定排序数组,输出二叉搜索树
思路1:需要使用二分法,找到最中间做为根节点,然后依次处理2侧,作为左右子树
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return helper(nums,0,nums.size()-1);
}
TreeNode* helper(vector<int>& nums, int left,int right){
if(left > right) return NULL;
int mid = left + (right-left)/2;
TreeNode * cur = new TreeNode(nums[mid]);
cur->left = helper(nums,left,mid-1);
cur->right = helper(nums,mid+1,right);
return cur;
}
};
#110 Balanced Binary Tree
要求:给定一个二叉树,判断是否为平衡二叉树
思路1:判断左右节点的高度差,超过1,false。不超过1时,需要继续往下层判断左右节点的高度;
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(!root) return true;
if(abs(getDepth(root->left)-getDepth(root->right)) >1) return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int getDepth(TreeNode *root) {
if (!root) return 0;
return 1 + max(getDepth(root->left), getDepth(root->right));
}
};
参考大神博客园:Grandyang
思路2:一直往下层,走到节点的最深层进行判断,查看子节点是否是平衡二叉树,如果任何节点不是的,直接返回-1即可,思路很巧妙,想不到
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(checkDepth(root) == -1) return false;
else return true;
}
int checkDepth(TreeNode* root)
{
if(!root) return 0;
int left = checkDepth(root->left);
if(left == -1) return -1;
int right = checkDepth(root->right);
if(right == -1) return -1;
int diff = abs(left - right);
if(diff > 1) return -1;
else return 1+ max(left,right);
}
};
菜鸟一枚,欢迎大家批评指正,谢谢~