375. Clone Binary TreeLintCode java

Posted phdeblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了375. Clone Binary TreeLintCode java相关的知识,希望对你有一定的参考价值。

Description

For the given binary tree, return a deep copy of it.

Example

Given a binary tree:

     1
   /    2    3
 / 4   5

return the new binary tree with same structure and same value:

     1
   /    2    3
 / 4   5

 

解题:链表复制。递归解法比较简单,代码如下:

/**
 * 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: The root of binary tree
     * @return: root of new tree
     */
    public TreeNode cloneTree(TreeNode root) {
        // write your code here
        if(root == null){
            return null;
        }
        TreeNode new_root = new TreeNode(root.val);
        new_root.left = cloneTree(root.left);
        new_root.right = cloneTree(root.right);
        return new_root;
    }
}

 

以上是关于375. Clone Binary TreeLintCode java的主要内容,如果未能解决你的问题,请参考以下文章

Boosting Static Representation Robustness for Binary Clone Search against Code Obfuscation and Compi

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

Boosting Static Representation Robustness for Binary Clone Search against Code Obfuscation and Compi

LeetCode --- 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree 解题报告

如何修复 pyinstaller 中的 no dbm clone found 错误