zoukankan      html  css  js  c++  java
  • 226 Invert Binary Tree 翻转二叉树

    翻转一棵二叉树。

         4
        /
       2   7
     /  /
    1  3 6  9
    转换为:

         4
        /
       7    2
     /    /
    9  6  3   1

    详见:https://leetcode.com/problems/invert-binary-tree/

    Java实现:

    递归实现:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public TreeNode invertTree(TreeNode root) {
            if(root==null){
                return null;
            }
            TreeNode node=root.left;
            root.left=invertTree(root.right);
            root.right=invertTree(node);
            return root;
        }
    }
    

    迭代实现:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public TreeNode invertTree(TreeNode root) {
            if(root==null){
                return null;
            }
            LinkedList<TreeNode> que=new LinkedList<TreeNode>();
            que.offer(root);
            while(!que.isEmpty()){
                TreeNode node=que.poll();
                TreeNode tmp=node.left;
                node.left=node.right;
                node.right=tmp;
                if(node.left!=null){
                    que.offer(node.left);
                }
                if(node.right!=null){
                    que.offer(node.right);
                }
            }
            return root;
        }
    }
    

    C++实现:

    方法一:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode* invertTree(TreeNode* root) {
            if(root==nullptr)
            {
                return nullptr;
            }
            TreeNode *node=root->left;
            root->left=invertTree(root->right);
            root->right=invertTree(node);
            return root;
        }
    };
    

    方法二:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode* invertTree(TreeNode* root) {
            if(root==nullptr)
            {
                return nullptr;
            }
            queue<TreeNode*> que;
            que.push(root);
            while(!que.empty())
            {
                TreeNode *node=que.front();
                que.pop();
                TreeNode *tmp=node->left;
                node->left=node->right;
                node->right=tmp;
                if(node->left)
                {
                    que.push(node->left);
                }
                if(node->right)
                {
                    que.push(node->right);
                }
            }
            return root;
        }
    };
    

      

  • 相关阅读:
    PyCharm配置 Git 教程
    Docker实践:基于python:3.7.1-stretch制作python镜像
    Docker开启远程安全访问
    Centos7安装apt-get
    Kubernetes 系列(二):在 Linux 部署多节点 Kubernetes 集群与 KubeSphere 容器平台
    微信小程序调试mock 数据,提示合法域名校验失败
    babel-plugin-import 配置多个组件按需加载时
    docker run -p 8070:80 -d nginx
    数据库的设计(E-R图,数据库模型图,三大范式)
    数据库 范式
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8758253.html
Copyright © 2011-2022 走看看