LintCode 649: Binary Tree Upside Down (类似链表反转好题)

89 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

原文链接:blog.csdn.net/roufoo/arti…

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.

Example Example1

Input: {1,2,3,4,5} Output: {4,5,2,#,#,3,1} Explanation: The input is 1 / 2 3 / 4 5 and the output is 4 / 5 2 / 3 1 Example2

Input: {1,2,3,4} Output: {4,#,2,3,1} Explanation: The input is 1 / 2 3 / 4 and the output is 4

2 / 3 1 {1,2,3,4,5}

解法1:递归。

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: the root of binary tree
     * @return: new root
     */
    TreeNode * upsideDownBinaryTree(TreeNode * root) {
        if (!root || !root->left) return root;
        TreeNode *left = root->left, *right = root->right;
        
        TreeNode * result = upsideDownBinaryTree(root->left);
        left->left = right;
        left->right = root;
        root->left = NULL;
        root->right = NULL;

        return result;
    }
};

解法2:递归。跟解法1差不多。效率高些,因为省了root->left=NULL和root->right=NULL这两个操作。实际上只有真正的root需要设这个。

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: the root of binary tree
     * @return: new root
     */
    TreeNode * upsideDownBinaryTree(TreeNode * root) {
        if (!root || !root->left) return root;
        //TreeNode *left = root->left, *right = root->right;
        
        TreeNode * result = dfs(root);
        root->left = NULL;
        root->right = NULL;

        return result;
    }

private:
    TreeNode * dfs(TreeNode * root) {
        if (!root || !root->left) return root;
        TreeNode *left = root->left;
        TreeNode *right = root->right;
        TreeNode * result = dfs(root->left);
        left->left = right;
        left->right = root;
        return result;
    }
};

解法3:非递归。类似反转链表的迭代法。 如下图所示,其中pre, root和next类似于链表反转的pre, head和temp。这里不同的是还要为右节点加个tmp。 最后返回pre。

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: the root of binary tree
     * @return: new root
     */
    TreeNode * upsideDownBinaryTree(TreeNode * root) {
        if (!root || !root->left) return root;
        
        TreeNode * pre = NULL;
        TreeNode * tmp = NULL; //for right child
        TreeNode * next = NULL; //for left child
        while(root) {
            next = root->left;
            root->left = tmp;
            tmp = root->right;
            root->right = pre;
            pre = root;
            root = next;
        }
        
        return pre;        
    }
};