求C#大神,LeetCode 617. Merge Two Binary Trees 怎么实现调用的,自己试着写了一下实现不了。
class Program
{
static void Main(string[] args)
{
TreeNode tree = new TreeNode(1);
Program p = new Program();
tree.val = 1;
TreeNode t1;
t1.val = 1;
TreeNode t2;
t2.val = 2;
//TreeNode t1 = { 1, 3, 2, 5 };
//TreeNode t2 = new TreeNode{2, 1, 3, null, 4, null, 7};
p.MergeTrees(t1, t2);
}
public TreeNode MergeTrees(TreeNode t1, TreeNode t2)
{
if (t1 == null && t2 == null)
{
return null;
}
if (t1 != null && t2 == null)
{
return t1;
}
if (t1 == null && t2 != null)
{
return t2;
}
if (t1 != null && t2 != null)
{
t1.val += t2.val;
t1.left = MergeTrees(t1.left, t2.left);
t1.right = MergeTrees(t1.right, t2.right);
}
return t1;
}
}
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
public int valprop {
get { return val;}
set { }
}
}