LeetCode SameTree

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        int v = ((p == NULL) << 1) + (q == NULL);
        if (v == 0x2 || v == 0x1) return false; // one NULL the other not NULL
        if (v == 0x3) return true;              // both NULL
        if (p->val != q->val) return false;     // value not match
        return isSameTree(p->left, q->left)     // check subtree
                    && isSameTree(p->right, q->right);
    }
};

继续水

LeetCode SameTree

时间: 2024-10-07 22:02:53

LeetCode SameTree的相关文章

★leetcode★ same-tree

题目要求: 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. 判断两棵二叉树是否相等 解题思路: 递归思想(好像大多数关于二叉树的题目都可以用递归来解决) 1 /** 2 * Def

LeetCode(100)题解--Same Tree

https://leetcode.com/problems/same-tree/ 题目: 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. 思路:  DFS AC代码: 1.递归 1

LeetCode:Same Tree - 判断两颗树是否相等

1.题目名称 Same Tree(判断两棵树是否相等) 2.题目地址 https://leetcode.com/problems/same-tree/ 3.题目内容 英文: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 h

LeetCode题目总结分类

注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/probl

小程序 - leetcode 100. Same Tree

https://leetcode.com/problems/same-tree/ /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ bool isSameTree(struct TreeNode* p, struct TreeNode* q) { if (p == NULL) retur

[LeetCode][Java]Same Tree

https://leetcode.com/problems/same-tree/ Same Tree 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. 1 /** 2 * Defin

[LeetCode]题解(python):100 Same Tree

题目来源 https://leetcode.com/problems/same-tree/ 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. 题意分析 Input: twon bin

【LeetCode】100. Same Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51541570 Subject 出处:https://leetcode.com/problems/same-tree/ Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structur

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法