主要是卡特兰特数的递归公式:
n>1
h(0) = 1;
0<=i<=n-1;左边是i个节点,右边是n-i-1个节点
h(n) += h(i) * h(n-i-1);
class Solution {
public:
int numTrees(int n) {
if(n == 0)
return 0;
int* num = new int[n+1];
num[0] = 1;
num[1] = 1;
for(int i = 2; i <= n; ++ i)
{
num[i] = 0;
for(int j = 0; j <= i-1; ++ j)
num[i] += num[j] * num[i-j-1];
}
return num[n];
}
};
二叉树的先序遍历的非递归形式
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
stack<TreeNode*> mystack;
vector<int> num;
if(root == NULL) return num;
mystack.push(root);
TreeNode* p;
while(!mystack.empty())
{
p = mystack.top();
mystack.pop();
num.push_back(p->val);
if(p->right) mystack.push(p->right);
if(p->left) mystack.push(p->left);
}
return num;
}
};
出现的错误应该是因为,栈中没有元素却出栈,所以不对,应该是两个判断条件root != NULL || !mystack.empty()
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
stack<TreeNode*> mystack;
vector<int> num;
if(root == NULL) return num;
while(root != NULL || !mystack.empty())
{
for(; root ; root = root->left)
mystack.push(root);
root = mystack.top();
num.push_back(root->val);
mystack.pop();
root = root->right;
}
return num;
}
};
后续遍历
需要注意的是,左右根的遍历,对于输出的节点需要满足两点 1)是叶子节点 2)其孩子都已经输出, 还有就是取节点,但节点暂时不出栈,等到条件满足再出栈
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
stack<TreeNode*> mystack;
vector<int> num;
if(root == NULL) return num;
mystack.push(root);
TreeNode* p=root;
TreeNode* out = root;
while(!mystack.empty())
{
p = mystack.top();
if((p->left == NULL && p->right == NULL) || p->left == out || p->right == out)
{
mystack.pop();
num.push_back(p->val);
out = p;
}else
{
if(p->right) mystack.push(p->right);
if(p->left) mystack.push(p->left);
}
}
return num;
}
};
二叉树的层次遍历,只不过每层放入一个vector中,就是做到层次区分
vector<vector<int> > levelOrder(TreeNode* root)
{
vector<vector<int> > result;
vector<int> temp;
queue<TreeNode* > myQueue;
TreeNode* p=root;
if(root == NULL) return result;
myQueue.push(root);
int pre = 1;
int cur = 0;
while(!myQueue.empty())
{
p = myQueue.front();
temp.push_back(p->val);
myQueue.pop();
--pre;
if(p->left != NULL)
{
myQueue.push(p->left);
++ cur;
}
if(p->right != NULL)
{
myQueue.push(p->right);
++ cur;
}
if(pre == 0)
{ result.push_back(temp);
temp.clear();
pre = cur;
cur = 0;
}
}
return result;
}
Flatten Binary Tree to Linked List
class Solution {
public:
TreeNode* flattenTree(TreeNode* node)
{
if(node == NULL)
return NULL;
if(node->left == NULL && node->right == NULL)
return node;
stack<TreeNode* > nodeStack;
TreeNode *temp = NULL, *head = node;
if(node->right!=NULL)
nodeStack.push(node->right);
if(node->left!=NULL)
nodeStack.push(node->left);
while(!nodeStack.empty())
{
temp = nodeStack.top();
nodeStack.pop();
node->right = temp;
node->left = NULL;
node = node->right;
if(temp->right!=NULL)
nodeStack.push(temp->right);
if(temp->left!=NULL)
nodeStack.push(temp->left);
}
return head;
}
void flatten(TreeNode *root)
{
if(root == NULL ||root->left == NULL && root->right == NULL)
return;
//树的先序遍历转化为链接在节点的右子树上的链表
flattenTree(root);
}
};