Binary Tree Upside Down

Given a binary tree where all the right nodes are either leaf nodes
with a sibling (a left node that shares the same parent node) or empty,
flip it upside down and turn it into a tree where the original right nodes
turned into left leaf nodes. Return the new root.

For example:
Given a binary tree {1,2,3,4,5},
  1
   / \
  2  3
   / \
  4  5
return the root of the binary tree [4,5,2,#,#,3,1].
   4
  / \
   5  2
  / \
 3  1

由于该树的特性,右子树只能是叶节点,因此使用一个栈就能记录从根节点到最左节点。

这些栈中的节点将逆序成为新的右子树的根节点。

原先的父节点成为右子节点,原先父节点的右子节点成为左子节点。

class Solution {
public:
TreeNode *upsideDownBinaryTree(TreeNode *root)
{
if(root == NULL)
return root; stack<TreeNode*> s; //left child list
s.push(root);
TreeNode* cur = root;
while(cur->left)
{
s.push(cur->left);
cur = cur->left;
}
TreeNode* newroot = s.top();
cur = newroot;
s.pop();
while(!s.empty())
{
TreeNode* oldfather = s.top();
s.pop();
cur->left = oldfather->right;
cur->right = oldfather;
cur = oldfather;
//reset
cur->left = NULL;
cur->right = NULL;
}
return newroot;
}
};

我设计的测试用例如下,全部通过:

int main()
{
Solution s;
TreeNode* n1 = new TreeNode();
TreeNode* n2 = new TreeNode();
TreeNode* n3 = new TreeNode();
TreeNode* n4 = new TreeNode();
TreeNode* n5 = new TreeNode(); //1. {} expect {}
TreeNode* ret = s.upsideDownBinaryTree(NULL);
if(ret == NULL)
cout << "1. pass" << endl;
else
cout << "1. fail" << endl;
//2. {1} expect {1}
n1 = new TreeNode();
ret = s.upsideDownBinaryTree(n1);
if(ret->val == && n1->left == NULL && n2->left == NULL)
cout << "2. pass" << endl;
else
cout << "2. fail" << endl;
//3. {1,2} expect {2,#,1}
n1 = new TreeNode();
n2 = new TreeNode();
n1->left = n2;
ret = s.upsideDownBinaryTree(n1);
if(ret->val == && ret->left == NULL && ret->right->val == && ret->right->left == NULL && ret->right->right == NULL)
cout << "3. pass" << endl;
else
cout << "3. fail" << endl;
//4. {1,2,3} expect {2,3,1}
n1 = new TreeNode();
n2 = new TreeNode();
n3 = new TreeNode();
n1->left = n2;
n1->right = n3;
ret = s.upsideDownBinaryTree(n1);
if(ret->val == && ret->left->val == && ret->left->left == NULL && ret->left->right == NULL && ret->right->val == && ret->right->left == NULL && ret->right->right == NULL)
cout << "4. pass" << endl;
else
cout << "4. fail" << endl;
//5. {1,2,#,3} expect {3,#,2,#,1}
n1 = new TreeNode();
n2 = new TreeNode();
n3 = new TreeNode();
n1->left = n2;
n2->left = n3;
ret = s.upsideDownBinaryTree(n1);
if(ret->val == && ret->left == NULL && ret->right->val == && ret->right->left == NULL && ret->right->right->val == && ret->right->right->left == NULL && ret->right->right->right == NULL)
cout << "5. pass" << endl;
else
cout << "5. fail" << endl;
//6. {1,2,3,4,5} expect {4,5,2,#,#,3,1}
n1 = new TreeNode();
n2 = new TreeNode();
n3 = new TreeNode();
n4 = new TreeNode();
n5 = new TreeNode();
n1->left = n2;
n2->left = n4;
n2->right = n5;
n1->right = n3;
ret = s.upsideDownBinaryTree(n1);
if(ret->val == && ret->left->val == && ret->left->left == NULL && ret->left->right == NULL && ret->right->val == && ret->right->left->val == && ret->right->left->left == NULL && ret->right->left->right == NULL && ret->right->right->val == && ret->right->right->left == NULL && ret->right->right->right == NULL)
cout << "6. pass" << endl;
else
cout << "6. fail" << endl;
//7. {1,2,#,3,4,5} expect {5,#,3,4,2,#,#,#,1}
n1 = new TreeNode();
n2 = new TreeNode();
n3 = new TreeNode();
n4 = new TreeNode();
n5 = new TreeNode();
n1->left = n2;
n2->left = n3;
n2->right = n4;
n3->left = n5;
ret = s.upsideDownBinaryTree(n1);
if(ret->val == && ret->left == NULL && ret->right->val == && ret->right->left->val == && ret->right->left->left == NULL && ret->right->left->right == NULL && ret->right->right->val == && ret->right->right->left == NULL && ret->right->right->right->val == && ret->right->right->right->left == NULL && ret->right->right->right->right == NULL)
cout << "7. pass" << endl;
else
cout << "7. fail" << endl;
}

【LeetCode】Binary Tree Upside Down的更多相关文章

  1. 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)

    [Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...

  2. 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)

    这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...

  3. 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)

    这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  4. 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)

    这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...

  5. 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)

    这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...

  6. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

    Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...

  7. 【leetcode】Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  8. 【leetcode】Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  9. 【leetcode】Binary Tree Zigzag Level Order Traversal (middle)

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

随机推荐

  1. chrome插件编写基本入门

    chrome插件编写基本入门  http://igeekbar.com/igeekbar/post/331.htm #精选JAVASCRIPTCHROME 作为一名程序猿,怎么能不会写chrome插件 ...

  2. mybatis自定义枚举转换类

    转载自:http://my.oschina.net/SEyanlei/blog/188919 mybatis提供了EnumTypeHandler和EnumOrdinalTypeHandler完成枚举类 ...

  3. Jenkins 集成Unity3D Xcode

    如果Mac 上没有安装brew.先安装:ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)& ...

  4. ios Url Encode

    //ios Url Encode //有时候在请求的参数里里特殊符号比如“+”等.而如果没有encode的话那么传过去的还是” ”,面实际上是%2B. -(NSString*)UrlValueEnco ...

  5. Swift语言精要 - 浅谈代理模式(Delegate)

    在iOS编程中,我们经常谈到代理代理,也就是delegate,那么什么是代理呢? 我们来看一下cocoa对它的描述: Delegation is Cocoa’s term for passing of ...

  6. [置顶] 在Visual Studio 2008上调试C语言程序

    C语言的地位和重要性就不用说了,但,很多人学习C语言,还在使用Visual C++ 6.0,甚至还有人使用Turbo C,很无语,只说一句吧:“OUT了". 让我们体验一下华丽的Visual ...

  7. SHELL AWK 循环求和

    1.简单求和,文件如下: [linux@test /tmp]$ cat test 123.52 125.54 126.36 求和: [linux@test /tmp]$ awk '{sum += $1 ...

  8. Spring MVC中Controller如何将数据返回给页面

    要实现Controller返回数据给页面,Spring MVC 提供了以下几种途径: ModelAndView:将视图和数据封装成ModelAndView对象,作为方法的返回值,数据最终会存到Http ...

  9. Java实战_手把手编写记事本

    Java运用SWT插件编写桌面记事本应用程序 可实现windows系统桌面记事本基本功能.傻瓜式教学,一步一步手把手操作.小白也可自己编写出完整的应用程序. 须要工具:Eclipse(带SWT插件) ...

  10. linux下切换python2和python3(转)

    0x00 为什么需要有两个版本的Python Python2和Python3不兼容是每个接触过Python的开发者都知道的事,虽说Python3是未来,但是仍然有很多项目采用Python2开发.Lin ...