zyphone的博客

想做程序猿的通信狗

LintCode:克隆二叉树

LintCode:克隆二叉树

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        this.val = val
        this.left, this.right = None, None
"""
class Solution:
    """
    @param {TreeNode} root: The root of binary tree
    @return {TreeNode} root of new tree
    """
    def cloneTree(self, root):
        # Write your code here
        if not root:
            return root

        my_root = TreeNode(root.val)
        self.pre_order_clone(my_root, root)
        return my_root

    def pre_order_clone(self, my_root, root):
        if not root:
            return
        if root.left:
            my_root.left = TreeNode(root.left.val)
            self.pre_order_clone(my_root.left, root.left)
        if root.right:
            my_root.right = TreeNode(root.right.val)
            self.pre_order_clone(my_root.right, root.right)
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012225151/article/details/52374495
个人分类: lintcode python
相关热词: lintcode
上一篇LintCode:平面列表
下一篇LintCode:最近公共祖先
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭