LeetCode100 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.(Easy)

分析:

二叉树问题往往采用递归解决比较简单,这个题就是单纯的递归问题,以此判断两个根是否为空,节点值是否相等,左右孩子是否存在且递归判定相同。

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSameTree(TreeNode* p, TreeNode* q) {
13         if (p == nullptr && q == nullptr) {
14             return true;
15         }
16         else if (p == nullptr && q != nullptr) {
17             return false;
18         }
19         else if (p != nullptr && q == nullptr) {
20             return false;
21         }
22         else {
23             if (p -> val != q -> val) {
24                 return false;
25             }
26             else {
27                 return (isSameTree(p -> left, q -> left) && isSameTree(p -> right, q -> right)) ;
28             }
29         }
30     }
31 };

 

时间: 2024-12-17 18:42:03

LeetCode100 Same Tree的相关文章

LeetCode日记3

(2015/11/3) LeetCode-36 Valid Sudoku:(easy) 1)使用数据结构 set<char> emptyset; vector<set<char>> mp(27, emptyset); 2)下标0-8存储行中的数字,9-17存储列中的数字,18-26存储3×3块中数字. 3)双重for循环中,i表示行,9 + j表示列,18 + i / 3 + 3 * (j / 3)表示块. (2015/11/12) LeetCode-38 Count

学习笔记:LeetCode100:Same Tree

LeetCode100: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. 判断两个二叉树是否相等,判断依据:结构相同,节点值相等 树节点包含当前值及分别指向其左右

easyui js取消选中 Tree 指定节点

取消所有选中 var rootNodes = treeObject.tree('getRoots'); for ( var i = 0; i < rootNodes.length; i++) { var node = treeObject.tree('find', rootNodes[i].id); treeObject.tree('uncheck', node.target); }

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

SPOJ375 Query on a tree

https://vjudge.net/problem/SPOJ-QTREE 题意: 一棵树,每条边有个权值 两种操作 一个修改每条边权值 一个询问两点之间这一条链的最大边权 点数<=10000 多组测试数据,case<=20 Example Input: 1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE Output: 1 3 #include<cstdio> #include<iostream> #include&

POJ 1741 Tree(树的点分治,入门题)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v)=The min distance between node u and v.Give an in

命令-tree

tree命令 tree - list contents of directories in a tree-like format. 显示目录的层级结构: tree 命令英文理解为树的意思,其功能是创建文件列表,将目录所有文件以树状的形式列出来.linux中的tree命令默认并不会安装,所以需要通过yum install tree -y来安装此命令. [SYNOPSIS] tree [options] [directory] [OPTIONS] -L level:指定要显示的层级: -d:仅列出目

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod