[LintCode] Binary Tree Flipping

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.

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  

Think Out Loud


For a given node n whose left child is not null, we do the following changes.

n.left.left = n.right;

n.left.right = n;

n.left = null;

n.right = null.

The new root after flipping is the leftmost node in the original tree.

We can solve this recursively bottom up. Solving it top down is more complicated because we need the top nodes to access

the down nodes. Changing the top nodes makes this process a lot harder.

O(n) runtime, O(H) space, H is the max depth of the give binary tree.

 1 public class Solution {
 2     public TreeNode upsideDownBinaryTree(TreeNode root) {
 3         if(root == null || root.left == null) {
 4             return root;
 5         }
 6         TreeNode new_root = upsideDownBinaryTree(root.left);
 7         root.left.left = root.right;
 8         root.left.right = root;
 9         root.left = null;
10         root.right = null;
11         return new_root;
12     }
13 }

Related Problems

Reverse Linked List

				
时间: 2024-12-20 01:11:57

[LintCode] Binary Tree Flipping的相关文章

LintCode: Binary Tree Preorder Traversal

C++,递归 1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * TreeNode *left, *right; 7 * TreeNode(int val) { 8 * this->val = val; 9 * this->left = this->right = NULL; 10 * } 11 * } 12 */ 13 14 class Solution { 15 pub

LintCode: Binary Tree Postorder Traversal

C++,递归 1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * TreeNode *left, *right; 7 * TreeNode(int val) { 8 * this->val = val; 9 * this->left = this->right = NULL; 10 * } 11 * } 12 */ 13 class Solution { 14 /** 15

[Lintcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). Example Given binary tree {3,9,20,#,#,1

LintCode Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return [ [3], [9,20], [15,7] ] For the problem given I decided to use BFS, howe

[lintcode] Binary Tree Inorder Traversal

Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. Example Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. SOLUTION 1: 递归方法,具体代码可以参照preorder traversal,这里就不赘述了. SOLUTION 2: 分治法,Divide & conquer,其

[LintCode] Binary Tree Vertical Order Traversal

Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). If two nodes are in the same row and column, the order should be from left to right. Have you met this question in a real inter

LintCode Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. Example Given the below binary tree: 1 / 2 3 return 6. For this problem we need to think about the problem in this way. Now we want the largest sum of

LintCode Binary Tree Paths

Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tree: 1 1 2 / 3 2 3 4 5 5 All root-to-leaf paths are: 1 [ 2 "1->2->5", 3 "1->3" 4 ] As mentioned in the problem we want to get al

[LintCode] Binary Tree Leaves Order Traversal

Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Have you met this question in a real interview? Yes Example Given binary tree: 1 / 2 3 / \ 4 5 Returns [[4, 5, 3], [