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. 【大数据】大数据处理-Lambda架构-Kappa架构

    大数据处理-Lambda架构-Kappa架构 elasticsearch-head Elasticsearch-sql client NLPchina/elasticsearch-sql: Use S ...

  2. offsetof与container_of宏[总结]

    1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...

  3. Creating OpenGL 4.3 window fails with GLFW3

      I set up a minimal application to open a blank window with GLFW3: #include <iostream> #inclu ...

  4. ASP入门(十三)-Server对象

    Server 对象用于处理服务器上的一些特殊任务,例如,创建组件实例.获取文件路径.执行ASP脚本文件等. Server 对象是体现 ASP 强大功能的一个对象,之前介绍的对象都是针对获取.请求以及简 ...

  5. Direct2D教程VII——变换几何(TransformedGeometry)对象

    目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...

  6. JavaScript:如何获得 Private、Privileged、Public 和 Static 成员(属性和方法)【翻译+整理】

    本文内容 背景 把我们的对象放在一起 添加一个私有(Private)的属性 添加一个特权(Privileged)的方法 添加一个公共(Public)的属性和方法 添加一个静态(Static)的属性 我 ...

  7. linux中fuser用法详解

    fuser功能 fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息.  fuser显示使用指定文件或者文件系统的进程ID.默认情况下每个文件名后面 ...

  8. Linux(CentOS)中使用Mono+jexus部署Asp.net4.5网站

    一.效果: 二.安装步骤: 1.安装系统CentOS,我这是用CentOs7测试的. 2.接下来安装libgdiplus.Mono.Jexus有问题可以参考安装工具的官网: Libgdiplus下载官 ...

  9. bootstrap自适应栅格系统布局

  10. SHELL字符串使用总结

    1.获取字符串的长度,${#str} #设置字符串 $ str="liqiu" #打印字符串 $ echo $str liqiu #继续打印字符串 $ echo ${str} li ...