【6_100】Same Tree

Same Tree

Total Accepted: 97481 Total Submissions: 230752 Difficulty: Easy

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

这次卡在了递归的return使用上,原来可以一次return两个啊

错误写法:

else if(p != NULL && q != NULL && p->val == q->val) {
  isSameTree(p->left, q->left) ;

  isSameTree(p->right, q->right);
}

正确写法:

else if(p != NULL && q != NULL && p->val == q->val) {
  return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
}

这次是C语言

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     struct TreeNode *left;
 6  *     struct TreeNode *right;
 7  * };
 8  */
 9 bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
10     if(p == NULL && q == NULL)
11         return true;
12     else if(p != NULL && q != NULL && p->val == q->val) {
13             return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
14     }
15     else
16         return false;
17 }
时间: 2024-08-17 00:52:46

【6_100】Same Tree的相关文章

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

【Leetcode】Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 思路:使用

【Leetcode】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

【HackerRank】Utopian tree

The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occurs during the monsoon, when it doubles in height. The second growth cycle of the tree occurs during the summer, when its height increases by 1 meter.

【BZOJ】2631: tree LCT

[题意]给定n个点的树,每个点初始权值为1,m次操作:1.x到y的点加值,2.断一条边并连一条边,保证仍是树,3.x到y的点乘值,4.x到y的点权值和取模.n,m<=10^5. [算法]Link-Cut Tree [题解]区间加和区间乘标记的处理:[BZOJ]1798: [Ahoi2009]Seq 维护序列seq 线段树 splay上维护要注意: 1.上传时加本身. 2.改值的时候不能影响到0点. 3.所有改变点的儿子的地方都要上传,所有改变点的父亲的地方都要下传. 除了rotate,还有acc

【题解】Digit Tree

[题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. 很明显可以点分治嘛,我们可以按照图上的样子,把一条路径本来是\(12345678\)的路径,变成\(1234|5678\),我们记录图中左边的那种路径为\(f\)(往根),右边的那种路径为\(g\)(从根),记右边的那种到分治中心的深度为\(d\),那么这条路径就可以被表示成\(f\times 1

【LeetCode】Binary Tree Right Side View 解题报告

[题目] Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3

【LeetCode】Binary Tree Maximum Path Sum 解题报告

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [解析] 题意:在二叉树中找一条路径,使得该路径的和最大.该路径可以从二叉树任何结点开始,也可以到任何结点结束. 思路:递归求一条经过root的最大路径,这条路径可能是:

【BZOJ】1468: Tree(POJ1741)

[题意]给定带边权树,求两点距离<=k的点对数.n<=40000. [算法]点分治 [题解]对于一个区域,选择其重心x作为根,则划分出来的每棵子树都是子区域,可以证明至多划分log n次(通过vis[]划分区域).每次划分所有点都扫描一次,所以仅遍历的复杂度是O(n log n). 对于本题,将点x的所有子树节点dis处理出来后排序,然后用双指针法易得<=k的点对数. 但是,这样会把来自同一子树的路径也计算进去,需要减去.来自同一子树y的距离<=k的路径的数量等同于子树y内路径的距