标签:type active int pre btn render www 二叉树 ble
原题网址:https://www.lintcode.com/problem/clone-binary-tree/description
深度复制一个二叉树。
给定一个二叉树,返回一个他的 克隆品 。
给定一个二叉树:
1
/ 2 3
/ 4 5
返回其相同结构相同数值的克隆二叉树:
1 / 2 3 / 4 5
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: The root of binary tree
* @return: root of new tree
*/
TreeNode * cloneTree(TreeNode * root) {
// write your code here
if (root==NULL)
{
return root;
}
TreeNode *newnode=new TreeNode(root->val);
newnode->left=cloneTree(root->left);
newnode->right=cloneTree(root->right);
return newnode;
}
};
标签:type active int pre btn render www 二叉树 ble
原文地址:https://www.cnblogs.com/Tang-tangt/p/9196445.html