MinimumDepth of Binary Tree
Question
Total Accepted: 91120 TotalSubmissions: 302986 Difficulty: Easy
Given a binarytree, find its minimum depth.
The minimumdepth is the number of nodes along the shortest path from the root node down tothe nearest leaf node.
Subscribe to see which companiesasked this question
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root) return 0;
else if(root->left==NULL&&root->right==NULL) return 1;
else if(root->left==NULL) return minDepth(root->right)+1;
else if(root->right==NULL) return minDepth(root->left)+1;
else return min(minDepth(root->left),minDepth(root->right))+1;
}
};
注:与树深度的算法相比,多了讨论左右子树不存在的情况!
Implement Stack using Queues
Implement thefollowing operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
class Stack {
public:
// Push element x onto stack.
queue<int> q;
void transfer()
{
int n = q.size();
int i = 0, x = 0;
for (i = 0; i < n-1; i++)
{
x = q.front();
q.pop();
q.push(x);
}
}
// Push element x onto stack.
void push(int x) {
q.push(x);
}
// Removes the element on top of the stack.
void pop() {
transfer();
q.pop();
}
// Get the top element.
int top() {
transfer();
int x = q.front();
q.pop();
q.push(x);
return x;
}
// Return whether the stack is empty.
bool empty() {
return q.empty();}
Palindrome Number
Determinewhether an integer is a palindrome. Do this without extra space.
class Solution {
public:
bool isPalindrome(int x) {
int sum=0,n=x;
if(x==0) return true;
while(x>0){
sum=sum*10+x%10;
x/=10;
}
if(n==sum) return true;
else return false;
}
};
注:把数字逆序,然后比较!
Path Sum
Given a binarytree and a sum, determine if the tree has a root-to-leaf path such that addingup all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, asthere exist a root-to-leaf path 5->4->11->2 which sum is 22.
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
queue<TreeNode*> q;
if(!root) return false;
q.push(root);
while(q.size()>0){
TreeNode *p=q.front();
if(p->left==NULL&&p->right==NULL)
if(p->val==sum) return true;
if(p->right) {
p->right->val=p->val+p->right->val;
q.push(p->right);
}
if(p->left){
p->left->val=p->val+p->left->val;
q.push(p->left);
}
q.pop();
}
return false;
}
};
Pascal's Triangle II
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<vector<int>> temp;
vector<int> s;
if(rowIndex<0) return s;
int k=-1;
for(int i=0;i<=rowIndex;i++){
vector<int> res;
for(int j=0;j<=i;j++){
if(j==0||j==i)
res.push_back(1);
else res.push_back(temp[i-1][j-1]+temp[i-1][j]);
}
temp.push_back(res);
k++;
}
return temp[k];
}
};
注:杨辉三角,重点!!二位数组的运用!
Binary TreeLevel Order Traversal
Given a binarytree, return the level order traversal of its nodes' values.(ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7},
3
/\
9 20
/ \
15 7
return its levelorder traversal as:
[
[3],
[9,20],
[15,7]
]
confused what "{1,#,2,3}" means? > read moreon how binary tree is serialized on OJ.
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
int count=1;
TreeNode* p;
if(!root) return res;
queue<TreeNode*> s;
s.push(root);
while(s.size()>0){
vector<int> s1;
while(count--){
p=s.front();
s1.push_back(p->val);
if(p->left)s.push(p->left);
if(p->right)s.push(p->right);
s.pop();
}
count=s.size();
res.push_back(s1);
}
return res;
}
};
注:重点掌握二维数组!