Binary Tree Level Order Traversal
链接:http://leetcode.com/onlinejudge#question_102
原题:
Given a binary tree, 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 level order traversal as:
[ [3], [9,20], [15,7] ]
思路:按照一层层遍历就可以了。
代码:
/**
* 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<vector<int> > levelOrder(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > collection;
if (root == NULL)
return collection;
vector<int> vec;
vector<vector<TreeNode*> > layers;
vector<TreeNode*> layer;
layer.push_back(root);
layers.push_back(layer);
while (true) {
vector<TreeNode*> &preLayer = layers.back();
vec.clear();
layer.clear();
for (int i=0; i<preLayer.size(); i++) {
if (preLayer[i]) {
vec.push_back(preLayer[i]->val);
layer.push_back(preLayer[i]->left);
layer.push_back(preLayer[i]->right);
}
}
if (vec.size() == 0)
break;
collection.push_back(vec);
layers.push_back(layer);
}
return collection;
}
};
Unique Binary Search Trees
链接:http://leetcode.com/onlinejudge#question_96
原题:
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
思路:只要求出递推式就可以了。
对于n个数字,记树的个数为treeNum[n];
树根只有n种可能,如果树根为i, 那么左子树有i-1个结点, 右子树有n-i个结点,
一共有treeNum[i-1] * treeNum[n-i]棵树, i = 1, ...., n, 累加一下就可以了.
代码:
class Solution {
public:
int numTrees(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (n == 0)
return 0;
vector<int> counter(n+1);
counter[0] = 1;
counter[1] = 1;
for (int k=2; k<=n; k++) {
int total = 0;
for (int i=1; i<=k; i++)
total += counter[i-1]*counter[k-i];
counter[k] = total;
}
return counter[n];
}
};
Populating Next Right Pointers in Each Node
链接:http://leetcode.com/onlinejudge#question_116
原题:
Given a binary tree
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1 / \ 2 3 / \ / \ 4 5 6 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
思路:因为是完全二叉树,所以简单多了。直接父亲节点往next遍历,把相应的子节点连接起来。
代码:
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (root == NULL || root->left == NULL)
return;
TreeLinkNode *head = root->left;
TreeLinkNode *parent = root;
head->next = parent->right;
TreeLinkNode *child = head->next;
while (parent->next) {
parent = parent->next;
child->next = parent->left;
parent->left->next = parent->right;
child = parent->right;
}
connect(head);
}
};
Populating Next Right Pointers in Each Node II
链接:http://leetcode.com/onlinejudge#question_117
原题:上一道题目的扩展版本,即对任意的二叉树进行connection
思路:其实是一回事情,父亲这一层按照next遍历,随带把子节点connect起来,再递归的做已经连起来的
子节点。
代码:
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (root == NULL)
return;
TreeLinkNode *head = getFirstChild(root);
if (head == NULL)
return;
TreeLinkNode *child = head;
if (root->right && child==root->left) {
child->next = root->right;
child = child->next;
}
while (root->next) {
root = root->next;
if (root->left) {
child->next = root->left;
child = child->next;
}
if (root->right) {
child->next = root->right;
child = child->next;
}
}
connect(head);
}
private:
TreeLinkNode* getFirstChild(TreeLinkNode *&root) {
while (root) {
if (root->left)
return root->left;
if (root->right)
return root->right;
root = root->next;
}
return NULL;
}
};