博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Binary Tree Upside Down
阅读量:6623 次
发布时间:2019-06-25

本文共 2419 字,大约阅读时间需要 8 分钟。

Problem Description:

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

This problem seems to be tricky at first glance. Well, let's analyze the above example carefully. We can immediately see that there is a reversed correspondence between the two trees, which are the 1 -> 2 -> 4 in the tree above and the 4 -> 2 -> 1 in the tree below. Moreover, for each node along the left path 1 -> 2 -> 4 in the original tree, its new left child is its original right sibling and its new right child is its original parent.

Now we can write down the following recursive code.

1 class Solution { 2 public: 3     TreeNode* upsideDownBinaryTree(TreeNode* root) { 4         return upsideDown(root, NULL, NULL);    // start from root 5     } 6 private: 7     TreeNode* upsideDown(TreeNode* node, TreeNode* parent, TreeNode* sibling) { 8         if (!node) return parent; 9         TreeNode* left = node -> left;          // old left child10         TreeNode* right = node -> right;        // old right child11         node -> left = sibling;                 // new left child is old right sibling12         node -> right = parent;                 // new right child is old parent13         return upsideDown(left, node, right);   // node is left's parent and right is left's sibling14     }15 };

The above recursive code can be turned into the following iterative code easily.

1 class Solution { 2 public: 3     TreeNode* upsideDownBinaryTree(TreeNode* root) { 4         TreeNode* run = root; 5         TreeNode* parent = NULL; 6         TreeNode* sibling = NULL; 7         while (run) { 8             TreeNode* left = run -> left;   // old left child 9             TreeNode* right = run -> right; // old right child10             run -> left = sibling;          // new left child is old right sibling11             run -> right = parent;          // new right child is old parent12             parent = run;13             run = left;14             sibling = right;15         }16         return parent;17     }18 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4572682.html

你可能感兴趣的文章
jQuery做个TextBox自动完成条
查看>>
IP地址规划
查看>>
【ZooKeeper Notes 4】可视化zookeeper的事务日志
查看>>
Globalize for Ruby on Rails
查看>>
Linux计划任务
查看>>
4-2 ADO.NET-查询和检索数据9
查看>>
用简单的命令检查电脑是否被安装木马
查看>>
Java正则表达式应用总结
查看>>
Office 365系列之八:配置和体验Exchange和Lync
查看>>
杂七杂八——C#实现二叉树,外带中序遍历
查看>>
NeHe OpenGL第十一课:飘动的旗帜
查看>>
C#枚举显示声明值类型
查看>>
在Windows下安装MongoDB
查看>>
《开发者突击:精通ASP.NET AJAX网络程序设计》终于面世
查看>>
最大市场份额的Unix-怎么管理Solaris服务器
查看>>
走开源信息化之路
查看>>
MySQL中实现分割字符串的方法
查看>>
关于平时服务器管理的疑难杂症点点滴滴
查看>>
拒绝只买现在,放弃未来,拒绝买工具型软件
查看>>
Symfony2Book07:创建和使用模板
查看>>