leetcode 100 Same Tree

原创 2016年05月30日 22:51:23

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.

Subscribe to see which companies asked this question


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool same(TreeNode *p, TreeNode *q) {
		if(p==NULL && q==NULL) return true;
		if(p==NULL || q==NULL) return false;
		if(p->val!=q->val) return false;
		if(!same(p->left, q->left)) return false;
		return same(p->right, q->right);
	}
	bool isSameTree(TreeNode *p, TreeNode *q) {
		return same(p, q);
	}
};




LeetCode 100 Same Tree(相同树判断)(二叉树、递归、栈和队列、深搜和宽搜)

翻译给定两个二叉树,写一个函数检查他们是否相等。两个二叉树如果结构上相同并且有相同的值,那么就认定他们是相等的。原文Given two binary trees, write a function t...
  • NoMasp
  • NoMasp
  • 2016年01月11日 13:27
  • 2208

【Leetcode】100.Same Tree

方法一:递归 思路: (1)若两棵树都为空,则返回true。 (2)若其中一棵树为空而另一棵树不为空,则返回false。 (3)否则判断根节点是否相等,以及左子树是否相同,右子树是否相同。 /** ...

LeetCode-100. Same Tree (Java)

Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
  • NOadu
  • NOadu
  • 2017年06月25日 11:47
  • 166

Leetcode 100. Same Tree

Given two binary trees, write a function to check if they are equal or not.Two binary trees are cons...

leetcode No100. Same Tree

Question: Given two binary trees, write a function to check if they are equal or not. Two bina...

[LeetCode]100.Same Tree

【题目】 Same Tree  Total Accepted: 4943 Total Submissions: 11464My Submissions Given two binary...

[Leetcode 100] Same Tree

Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

【LeetCode OJ 100】Same Tree

题目:Given two binary trees, write a function to check if they are equal or not. Two binary trees are...

[leetcode:python]100.Same Tree

题目: Given two binary trees, write a function to check if they are equal or not.Two binary trees are...
  • NXHYD
  • NXHYD
  • 2017年05月11日 10:01
  • 149

【Leetcode】100. Same Tree

Question: Given two binary trees, write a function to check if they are equal or not. Two bin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 100 Same Tree
举报原因:
原因补充:

(最多只允许输入30个字)