Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 6, 2016 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/2c55d67226b86d38f4158160801b82ff to your computer and use it in GitHub Desktop.
Save jianminchen/2c55d67226b86d38f4158160801b82ff to your computer and use it in GitHub Desktop.
is Same tree -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sameTree_fCodeLab
{
/*
* Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Return 0 / 1 ( 0 for false, 1 for true ) for this problem
Example :
Input :
1 1
/ \ / \
2 3 2 3
Output :
1 or True
*/
class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x)
{
val = x;
}
}
class Program
{
static void Main(string[] args)
{
}
public static int isSameTree(TreeNode a, TreeNode b)
{
if (a == null && b == null)
return 1;
if (a == null)
return 0;
if (b == null)
return 0;
if (a.val != b.val)
return 0;
return isSameTree(a.left, b.left) & isSameTree(a.right, b.right);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment