j**********3 发帖数: 3211 | |
c*******e 发帖数: 621 | 2 read4网上到处有啊 本来就不是它家原创
Binary Tree Upside Down 顾名思义 |
c*******e 发帖数: 621 | |
w**p 发帖数: 4080 | |
b******g 发帖数: 3616 | 5 就是这道。题目毕竟不是他家原创的,觉得没什么泄露不泄露的。
我的理解是这15/49刀买的是书,以及那10题使用OJ来测试你解答正确与否的service费
。这两样是LC自己的东西。
【在 c*******e 的大作中提到】 : Binary Tree Upside Down 我猜是这个 : http://www.careercup.com/question?id=6266917077647360
|
j**********3 发帖数: 3211 | 6 Binary Tree Upside Down怎么作?
【在 c*******e 的大作中提到】 : read4网上到处有啊 本来就不是它家原创 : Binary Tree Upside Down 顾名思义
|
j**********3 发帖数: 3211 | |
b******g 发帖数: 3616 | 8 还没看过答案,我写了个递归做法,可能还有更好的解。递归的思路:
假设当前节点为r,需要先递归将r->left为根的子树upside down并返回最右叶子节点n
。然后将r->right接到n->left,将r接到n->right。由于此时新树的最右叶子节点为r
,返回r供上层递归使用。代码里的newRoot是为了返回整个树upside down后的新的根。
class Solution {
public:
TreeNode *upsideDownBinaryTree(TreeNode *root) {
TreeNode *temp, *newRoot = NULL;
temp = buildUpsideDownBT(root, newRoot);
return newRoot;
}
TreeNode *buildUpsideDownBT(TreeNode *root, TreeNode *&newRoot) {
if(!root) return root;
if(!root->left && !root->right) {
newRoot = root;
return root;
}
TreeNode *parent = buildUpsideDownBT(root->left, newRoot);
parent->left = root->right;
parent->right = root;
root->left = root->right = NULL;
return parent->right;
}
};
【在 j**********3 的大作中提到】 : 1337 大牛不乖了,哼哼
|
j*****y 发帖数: 9 | 9 好思路
点n
r
根。
【在 b******g 的大作中提到】 : 还没看过答案,我写了个递归做法,可能还有更好的解。递归的思路: : 假设当前节点为r,需要先递归将r->left为根的子树upside down并返回最右叶子节点n : 。然后将r->right接到n->left,将r接到n->right。由于此时新树的最右叶子节点为r : ,返回r供上层递归使用。代码里的newRoot是为了返回整个树upside down后的新的根。 : class Solution { : public: : TreeNode *upsideDownBinaryTree(TreeNode *root) { : TreeNode *temp, *newRoot = NULL; : temp = buildUpsideDownBT(root, newRoot); : return newRoot;
|