博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lintcode :Invert Binary Tree 翻转二叉树
阅读量:6922 次
发布时间:2019-06-27

本文共 2983 字,大约阅读时间需要 9 分钟。

题目:

翻转一棵二叉树

样例
1         1 / \       / \2   3  => 3   2   /       \  4         4
挑战

递归固然可行,能否写个非递归的?

解题:

递归比较简单,非递归待补充

Java程序:

/** * Definition of TreeNode: * public class TreeNode { *     public int val; *     public TreeNode left, right; *     public TreeNode(int val) { *         this.val = val; *         this.left = this.right = null; *     } * } */public class Solution {    /**     * @param root: a TreeNode, the root of the binary tree     * @return: nothing     */    public void invertBinaryTree(TreeNode root) {        // write your code here        invertTree(root);            }    public TreeNode invertTree(TreeNode root){        if( root ==null){            return root;        }        TreeNode rleft= root.left;        root.left = invertTree(root.right);        root.right = invertTree(rleft);        return root;    }}
View Code

总耗时: 994 ms

Python程序:

"""Definition of TreeNode:class TreeNode:    def __init__(self, val):        self.val = val        self.left, self.right = None, None"""class Solution:    # @param root: a TreeNode, the root of the binary tree    # @return: nothing    def invertBinaryTree(self, root):        # write your code here        self.invertTree(root)            def invertTree(self,root):        if root == None:            return root        rlft = root.left        root.left = self.invertTree(root.right)        root.right = self.invertTree(rlft)        return root
View Code

总耗时: 118 ms

参考剑指OfferP127改进了下程序,更容易理解了

/** * Definition of TreeNode: * public class TreeNode { *     public int val; *     public TreeNode left, right; *     public TreeNode(int val) { *         this.val = val; *         this.left = this.right = null; *     } * } */public class Solution {    /**     * @param root: a TreeNode, the root of the binary tree     * @return: nothing     */    public void invertBinaryTree(TreeNode root) {        // write your code here        invertTree(root);    }    public void invertTree(TreeNode root){        if( root ==null){            return;        }        if(root.left == null && root.right ==null){            return;        }        TreeNode tmp= root.left;        root.left = root.right;        root.right = tmp;        if(root.left!=null){            invertTree(root.left);        }        if(root.right!=null){            invertTree(root.right);        }    }}
Java Code
 
"""Definition of TreeNode:class TreeNode:    def __init__(self, val):        self.val = val        self.left, self.right = None, None"""class Solution:    # @param root: a TreeNode, the root of the binary tree    # @return: nothing    def invertBinaryTree(self, root):        # write your code here        if root == None:            return        if root.left==None and root.right ==None:            return        tmp = root.left        root.left = root.right        root.right = tmp        if root.left!=None:            self.invertBinaryTree(root.left)        if root.right!=None:            self.invertBinaryTree(root.right)
Python Code

 

转载地址:http://tdkjl.baihongyu.com/

你可能感兴趣的文章
字符串ucwords解析
查看>>
Windows2003 sp2 R2 的序列号及15种版本
查看>>
我的友情链接
查看>>
用C#实现对MSSqlServer数据库的增删改查---DAL层
查看>>
windows azure虚拟机里面安装FTP服务器(serv-u)之azure设置
查看>>
MSSQL内网***笔记
查看>>
mongodb仲裁节点
查看>>
Centos 7 更换yum源
查看>>
python冒泡算法
查看>>
我的友情链接
查看>>
sed小摘抄
查看>>
查看计算机GUID的WMI类
查看>>
关于VS和Dev运行结果的DOS界面一闪而过的处理
查看>>
如何成为高级DBA(上)
查看>>
【CentOS 7.1】时间设置
查看>>
无线技巧:学会设置无线上网猫以及网卡
查看>>
DecimalFormat 用法(数字格式化)
查看>>
android 设置seekBar 和Progress背景色
查看>>
JSP 9个内置对象详解
查看>>
6.MyBatis实现一对一查询
查看>>