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

本文介绍了一种将特定形态的二叉树翻转的算法,通过递归和非递归方式实现,使右子节点变为左叶节点,返回新的根节点。提供了三种解法,包括两种递归方法和一种迭代方法。
  1. 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;        
    }
};
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值