LeetCode -- 二叉树水题II
来源:互联网 发布:空军知乎 编辑:程序博客网 时间:2024/05/29 16:18
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;}};
- LeetCode -- 二叉树水题II
- 【LeetCode】Path Sum II 二叉树递归
- Leetcode中几道二叉树题 II
- leetcode--BinaryTreeLevelOrderTraversal II (二叉树层遍历递归实现)
- LeetCode 113: Path Sum II 二叉树遍历
- LeetCode-95-Unique Binary Search Trees II 二叉树
- 【LeetCode】BinaryTreeLevelOrderTraversal_ii 二叉树的层次遍历 II
- LeetCode | Binary Tree Level Order Traversal II(二叉树层序遍历II)
- LeetCode——二叉树水题
- Leetcode 117 Populating Next Right Pointers in Each Node II 二叉树填充next指针指向右侧结点 II
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】
- Path Sum II 二叉树路径和,打印所有路径 @LeetCode
- Binary Tree Level Order Traversal II 二叉树按层遍历(反向输出)@LeetCode
- Unique Binary Search Trees II 输出二叉树的所有组合@LeetCode
- leetCode解题报告之Binary Tree Level Order Traversal II,I(二叉树层次遍历)
- LeetCode | Unique Binary Search Trees II(构建二叉搜索树)
- 【leetcode 二叉树路径和】Path Sum 和 Path Sum II
- LeetCode OJ 之 Binary Tree Level Order Traversal II (二叉树的层次遍历-二)
- 常用库函数编程实现用法总结(三) strchr strrev memset strtod
- three.js学习
- hdu 1233
- hibernate性能调优(下)
- 函数模板的重载(overload)
- LeetCode -- 二叉树水题II
- DFS 解 滑雪问题
- 杭电题目做的情况
- 搜索背后的奥秘——浅谈语义主题计算
- flex4 图片/组件 拖放的简单实现
- Passport验证
- 数组实现大数阶乘
- BNU28000 Maxim and Restaurant
- GCC编译多个C文件