目录
107. 二叉树的层次遍历 II
binary-tree-level-order-traversal-ii 68.52% 100.00%
/**
* 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 {
private:
vector<vector<int>> ans;
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
if(root==NULL) return {};
vector<vector<int>> tmp;
queue<TreeNode *> qu;
qu.push(root);
int level=0;
while(!qu.empty()){
int length=qu.size();
tmp.push_back(vector<int>());
for(int i=0;i<length;i++){
TreeNode *cur=qu.front();qu.pop();
if(cur->left) qu.push(cur->left);
if(cur->right) qu.push(cur->right);
tmp[level].push_back(cur->val);
}
level++;
}
int len=tmp.size(),j=0;
ans=vector<vector<int>>(len);
for(int i=len-1;i>=0;i--){
ans[j++]=tmp[i];
}
return ans;
}
};
108. 将有序数组转换为二叉搜索树
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。
本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
示例:
给定有序数组: [-10,-3,0,5,9],
一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树:
0
/ \
-3 9
/ /
-10 5
/**
* 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 {
private:
vector<int> num;
TreeNode * insert(int left,int right){
if(left>right) return NULL;
int mid=(left+right)/2;
TreeNode * cur=new TreeNode(num[mid]);
cur->left=insert(left,mid-1);
cur->right=insert(mid+1,right);
return cur;
}
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
//把 n-1/2 作为 root
num=nums;
if(!nums.size()) return NULL;
return insert(0,nums.size()-1);
}
};
class Solution {
int[] nums;
public TreeNode helper(int left, int right) {
if (left > right) return null;
// always choose left middle node as a root
int p = (left + right) / 2;
// inorder traversal: left -> node -> right
TreeNode root = new TreeNode(nums[p]);
root.left = helper(left, p - 1);
root.right = helper(p + 1, right);
return root;
}
public TreeNode sortedArrayToBST(int[] nums) {
this.nums = nums;
return helper(0, nums.length - 1);
}
}
110. 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
示例 1:
给定二叉树 [3,9,20,null,null,15,7]
3
/ \
9 20
/ \
15 7
返回 true 。
28.44% 100.00%
class Solution {
private:
bool ans;
int dfs(TreeNode *cur){
if(!cur->left && !cur->right) return 1;
int a1,a2;
if(cur->left) a1=dfs(cur->left);else a1=0;
if(cur->right) a2=dfs(cur->right);else a2=0;
if(a1>a2){int tmp=a1;a1=a2;a2=tmp;}
if(a2-a1>1) ans=false;
return max(a1,a2)+1;
}
public:
bool isBalanced(TreeNode* root) {
if(root==NULL) return true;
ans=true;
int x=dfs(root);
return ans;
}
};
111. 二叉树的最小深度
62.24% 100.00%
class Solution {
private:
int min_h;
void dfs(TreeNode * root,int cur_h){
if(!root->left && !root->right){
if(cur_h<min_h){
min_h=cur_h; return ;
}
}
if(root->left) dfs(root->left,cur_h+1);
if(root->right) dfs(root->right,cur_h+1);
}
public:
int minDepth(TreeNode* root) {
if(root==NULL) return 0;
min_h=0x3f3f3f;
dfs(root,1);
return min_h;
}
};
112. 路径总和
62.36%100.00%
class Solution {
private:
bool res;
void dfs(TreeNode * root,int cur_sum,int sum){
if(res) return ;
if(cur_sum==sum ){
if(!root->left && !root->right){
res=true; return ;
}
}
if(root->left) dfs(root->left,cur_sum+root->left->val,sum);
if(root->right) dfs(root->right,cur_sum+root->right->val,sum);
}
public:
bool hasPathSum(TreeNode* root, int sum) {
if(!root && sum>=0) return false;
res=false;
dfs(root,root->val,sum);
return res;
}
};