简化写法,直接递归原函数isSameTree,首先:① 取消子函数dfs ② 无需分别判断 node1 & node2 的NULL情况 classSolution {public:boolisSameTree(TreeNode* p, TreeNode*q) {if(p&&q)//p & q 均有值return(p->val==q->val && isSameTree(p->left,q->left) && isSameTree(p->right,q->right))...
通过的Java代码如下: /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/classSolution{publicbooleanisSameTree(TreeNodep,TreeNodeq){if(p==null&&q==null){returntrue;}elseif(p==null&&q!
给定两个树,判断两个数是否相同 Solution1: DFS (Code will be neat and simple) code 1classSolution {2publicbooleanisSameTree(TreeNode p, TreeNode q) {3if(p ==null&& q ==null)returntrue;4if(p ==null|| q ==null)returnfalse;56if(p.val ==q.val){7returnisSameTree(p.left, q.left...
* int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ #include <queue> class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { queue<TreeNode*> Q1,Q2; TreeNode *pCurr,*qCurr; int flag = true;...
【LeetCode 100. Same Tree】(判断二叉树是否相等) 题目链接 Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes have the same value....
* 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) { if(!p && !q) { return true; } else if(p && q) { return ...
1519. 子树中标签相同的节点数 - 给你一棵树(即,一个连通的无环无向图),这棵树由编号从 0 到 n - 1 的 n 个节点组成,且恰好有 n - 1 条 edges 。树的根节点为节点 0 ,树上的每一个节点都有一个标签,也就是字符串 labels 中的一个小写字符(编号为 i 的 节点的标签就
题目链接[https://leetcode-cn.com/problems/same-tree/]tag: Easy; DFS; BFS; question: Given ...
Q100 Same Tree Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes have the same value. Example 1: Input:11/\/\2323[1,2,3],[1,2,3]Output:true...
Given the roots of two binary treespandq, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. Example 1: Input:p = [1,2,3], q = [1,2,3]Output:true ...