1、Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
1、给定一棵二叉树,找到它的最小深度
(最小深度是从根节点出发离其最近的叶子结点的深度)
#include<iostream>
using namespace std;
typedef struct TreeNode{
int data;
struct TreeNode *left;
struct TreeNode *right;
}TreeNode;
int run(TreeNode *root) {
int m,n;
if(root == NULL)
return 0;
m=run(root->left);
n=run(root->right);
if(m==0||n==0)
return 1+m+n;
return 1+min(m,n);
}
TreeNode* insert(TreeNode *root,int key){
if(root == NULL)
{
root=(TreeNode*)malloc(sizeof(TreeNode));
root->data=key;
root->left=NULL;
root->right=NULL;
return root;
}
if(root->data>key)
root->left=insert(root->left,key);
if(root->data<key)
root->right=insert(root->right,key);
return root;
}
void print(TreeNode *root){
if(root==NULL)
return;
cout<<root->data;
print(root->left);
print(root->right);
}
int main(){
int a[10]={5,4,6,3,8,2,9,0,1,7};
TreeNode *root=NULL;
for(int i=0;i<10;i++){
root=insert(root,a[i]);
}
cout<<"二叉排序树的先序遍历:"<<endl;
print(root);
cout<<endl;
int m=run(root);
cout<<"最小深度为:"<<m<<endl;
system("pause");
return 0;
}
2、
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are+,-,*,/. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
//计算一个抽象的逆波兰式的值
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> s;
for(auto x: tokens)//遍历tokens中的每一个字符
{
if(x=="+"||x=="-"||x=="*"||x=="/")
//若为+-*/则取栈顶元素进行相应操作,并将结果push进栈中
{
if(s.size()<2) return 0;
int a=s.top();s.pop();
int b=s.top();s.pop();
int c=0;
if(x=="+")
c=a+b;
else if(x=="-")
c=b-a;
else if(x=="*")
c=b*a;
else if(x=="/")
c=b/a;
s.push(c);
}
else//否则的话,将其为操作数将其push进栈中
{
s.push(atoi(x.c_str()));
}
}
return s.top();
}
};
3、以O(nlogn)的时间复杂度对一个链表进行排序,使用常数空间复杂度
使用归并排序的算法,先利用快慢指针找到中点,再利用归并排序进行排序
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *sortList(ListNode *head) {
//当头结点或者头结点为空时
if (!head || !head->next) return head;
//快慢指针找中点,快指针一次走两步,慢指针一次走一步
ListNode* p = head, *q = head->next;
while(q && q->next) {
p = p->next;
q = q->next->next;
}
ListNode* left = sortList(p->next);
p->next = NULL;
ListNode* right = sortList(head);
//合并链表
return merge(left, right);
}
ListNode *merge(ListNode *left, ListNode *right) {
//进行归并
ListNode dummy(0);
ListNode *p = &dummy;
while(left && right) {
if(left->val < right->val) {
p->next = left;
left = left->next;
}
else {
p->next = right;
right = right->next;
}
p = p->next;
}
if (left) p->next = left;
if (right) p->next = right;
return dummy.next;
}
};
4、
哈夫曼树,第一行输入一个数n,表示叶结点的个数。 需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight, 题目需要输出所有结点的值与权值的乘积之和。
解析:带权路径长度之和=(所有的叶结点)的权值*路径长度之和,根据分析可知其等于每一个非叶节点的权值之和。因此求哈夫曼树的带权路径长度之和就是求各非叶节点的权值之和。
#include <iostream>
using namespace std;
int Min(int *arr,int &n)//每次都返回最小值
{
int i,k,min;
min=arr[0];
k=0;
for(i=0;i<n;i++)
{
if(arr[i]<min)//找出最小值,并修改k值,最小值的位置赋值给k值
{
min=arr[i];
k=i;
}
}
arr[k]=arr[--n];//用最后一个元素去填充最小值处,相当于输出了最小值
return min;
}
int main()
{
int n;
while(cin>>n)
{
int b[1000];//存放叶节点的数组
int i;
for(i=0;i<n;i++)
cin>>b[i];
int sum,wgt;
sum=0;
wgt=0;
while(n>1)
{
sum=Min(b,n)+Min(b,n);//取最小的两个数相加,得到值为sum,非叶结点的权值
wgt+=sum;//带权路径长度之和就是所有非叶节点之和
b[n++]=sum;//将新得到的重新加入数组b中
}
cout<<wgt<<endl;
}
}
5、查找一个数组的第K小的数,注意同样大小算一样大,如2 1 3 4 5 6第三小数为3
解析:先利用冒泡排序,将原数组进行排序,再找第k小的数
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;//n个数
int k;
int a[1000];
for(int i=0;i<n;i++)
{//输入数组a
cin>>a[i];
}
cin>>k;
if(k<=n)
{
int t;
for(int j=0;j<n;j++)
{
for(int m=n-1;m>j;m--)
{
if(a[m-1]>a[m])
{
t=a[m];
a[m]=a[m-1];
a[m-1]=t;
}
}
}
int count=0;
for(int l=0;l<n;l++){
if(a[l]!=a[l+1]) count++;
if(count==k)
{
cout<<a[l];
break;}
}
}
else exit(1);
system("pause");
return 0;
}
6、Sort a linked list using insertion sort (采用直接插入排序对一个链表进行排序)
分析:先构造一个新的节点,作为新链表的头结点,结点值赋值为0。
对于新链表(有序链表),采用两指针遍历法,p在前,q在后,寻找适合的插入位置,即while(q->val<r->val)时说明待插入的元素值比q指向节点的值要大,所以p指针和q指针均后移
对于旧链表,采用head指针进行遍历整个链表,使用r指针指向待插入的元素,head指针确保在插入过程中不断链。
具体实现代码如下:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(head ==NULL)
return head;
ListNode node(0);//创建一个新的头结点,头结点的值为0,头结点作为新链表的表头结点
ListNode* p,*q,*r;
while(head)
{
p=&node;//使p指针指向新链表的链首位置
q=p->next;//q指针实际上指向的是有序链表的第一个元素
r=head;//使用r指针标记原链表待插入的节点
head=head->next;//head指针后移保证不断链
while(q&&q->val<r->val)
{
p=p->next;
q=q->next;
}//找到合适的位置后进行插入
r->next=q;
p->next=r;
}
return node.next;
}
};