zoukankan      html  css  js  c++  java
  • 156.Binary Tree Upside Down

    // Recursion
    class Solution {
    public:
        TreeNode *upsideDownBinaryTree(TreeNode *root) {
            if (!root || !root->left) return root;
            TreeNode *l = root->left, *r = root->right;
            TreeNode *res = upsideDownBinaryTree(l);
            l->left = r;
            l->right = root;
            root->left = NULL;
            root->right = NULL;
            return res;
        }
    };
    
    // Iterative
    class Solution {
    public:
        TreeNode *upsideDownBinaryTree(TreeNode *root) {
            TreeNode *cur = root, *pre = NULL, *next = NULL, *tmp = NULL;
            while (cur) {
                next = cur->left;
                cur->left = tmp;
                tmp = cur->right;
                cur->right = pre;
                pre = cur;
                cur = next;
            }
            return pre;
        }
    };
    
  • 相关阅读:
    Mybatis学习笔记14
    Mybatis学习笔记13
    Mybatis学习笔记12
    poj 2513
    poj 2001
    poj 1080
    poj 1703
    poj 1521
    poj 1384
    poj 1726
  • 原文地址:https://www.cnblogs.com/smallredness/p/10682104.html
Copyright © 2011-2022 走看看