problem:
Given a binary tree where all the right nodes is either leaf nodes with a sibling (a left node that shares the same paren T node) or empty, flip it upside down and turn it to a tree where the original right nodes turned to 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
Confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
Wrong solution:
Public classSolution { PublicTreeNode upsidedownbinarytree (TreeNode root) {if(Root = =NULL) returnRoot; TreeNode Upsided_left=Upsidedownbinarytree (Root.left); TreeNode Upsided_right=Upsidedownbinarytree (root.right); if(Upsided_left = =NULL) returnRoot; Root.left=NULL; Root.right=NULL; TreeNode Right_most=Upsided_left; if(Right_most.right! =NULL) Right_most=Right_most.right; Right_most.left=Upsided_right; Right_most.right=Root; returnUpsided_left; }}
MistakesAnalysis:
mistake analysis:input:[1, 2,NULL, 3,NULL, 4]output:[4,NULL, 3,NULL, 1]expected:[4,NULL, 3,NULL, 2,NULL, 1]the above code is complicated and wrong. Cause I Don' t throughly understand the logic behind this problem. I evenTryto find the right insert position at the upsided result, which is a indicator of wrong. Actually the idea is really isn't hard. For a tree, we must sure,1. Right child must is a leaf node or empty.2. The current left subtree would become theNewRoot (see it as a single node). I understand the above and the important points. But I Miss ThisOne .3. Iff the left subtree are not a single node (the root of the sub-tree would become its upsidedtree ' s rightmost node, Where we should attach the root as left child and Root as right child) Very IMPORTANT!!!!Thus above solution is complex and Wrong.fix1:treenode upsided_right =Upsidedownbinarytree (root.right); Since right child must was a leaf or empty, there is no need to perform upsidedownbinarytree over It.fix2:don ' t try to search for rightmost node manually, the left child of the current tree have already been turned into the right m OST node. TreeNode Right_most =Upsided_left;if(Right_most.right! =NULL) Right_most=Right_most.right;right_most.left=Upsided_right;right_most.right=root;root pointer have not been updated after"Upsidedownbinarytree (Root.left)", Root.left still point to it's left child before upsided. (Root ' s informaiton have not been changed yet!!!) Root.left.left=Root.right;root.left.right= root;
You should also pay attention to the base case of this solution.
It could be null pointer or leaf node
Null Pointer:root = = NULL
Leaf Node:root.left = = null && Root.right = = NULL
In the case we need the continue to search at Next level, this is only the left child.
Solution:
Public classSolution { PublicTreeNode upsidedownbinarytree (TreeNode root) {if(Root = =NULL|| Root.left = =NULL&& Root.right = =NULL) returnRoot; TreeNode Newroot=Upsidedownbinarytree (Root.left); Root.left.left=Root.right; Root.left.right=Root; Root.left=NULL; Root.right=NULL; returnNewroot; }}
[leetcode#156] Binary Tree Upside Down