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.

Hide Tags

Tree Depth-first Search

/**
 * 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) {
        if(q==NULL && p==NULL)
            return true;
        if(q==NULL || p==NULL)
            return false;
        return p->val==q->val && isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
    }
};
时间: 2024-08-02 22:42:14

Same Tree 深度优先的相关文章

Symmetric Tree 深度优先搜索

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could solve it both recu

leetcode笔记:Minimum Depth of Binary Tree

一. 题目描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 二. 题目分析 这道题属于二叉树的深度优先搜索,然后返回深度最小的值,可以递归(当然,也可以使用迭代)来实现.递归退出的条件是到达叶子节点或者到达空子树,使用空子树

LeetCode_Binary Tree Maximum Path Sum

一.题目 Binary Tree Maximum Path Sum Total Accepted: 41576 Total Submissions: 193606My Submissions 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 Retu

Maximum Depth of Binary Tree -- leetcode

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. 基本思路: 深度优先遍历. 在leetcode上实行执行时间为18ms. /** * Definition for a binary tree node. * struct Tre

树的深度优先遍历和广度优先遍历

1 import java.util.ArrayDeque; 2 3 public class BinaryTree { 4 static class TreeNode{ 5 int value; 6 TreeNode left; 7 TreeNode right; 8 9 public TreeNode(int value){ 10 this.value=value; 11 } 12 } 13 14 TreeNode root; 15 16 public BinaryTree(int[] ar

C++ Binary Tree Traversal

关于二叉树的遍历有很多的方法, 下面介绍两个经典的遍历算法: BFS和DFS.一个是深度优先遍历, 一个是广度有优先遍历. 这两种遍历算法均属于盲目的遍历算法, 一般而言, 启发式的遍历搜索算法比较好一些. . 关于各种遍历算法的对比, 将会在后面逐一提及. 这里不在赘述. 由于树是一个非线性的数据结构, 显然不能像linked list , 或者Array那样通过从头像最末尾移动去实现遍历每一个元素, 我们使用的是BFS和DFS算法. 例如下面的一个二叉树: 所谓的树遍历(Tree trave

笔试算法题(50):简介 - 广度优先 & 深度优先 & 最小生成树算法

广度优先搜索&深度优先搜索(Breadth First Search & Depth First Search) BFS优缺点: 同一层的所有节点都会加入队列,所以耗用大量空间: 仅能非递归实现: 相比DFS较快,空间换时间: 适合广度大的图: 空间复杂度:邻接矩阵O(N^2):邻接表O(N+E): 时间复杂度:O(V+E): DFS优缺点: 无论是系统栈还是用户栈保存的节点数都只是树的深度,所以空间耗用小: 有递归和非递归实现: 由于有大量栈操作(特别是递归实现时候的系统调用),执行速度

Leetcode 257 Binary Tree Paths 二叉树 DFS

找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. 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 p

LeetCode:Binary Tree Paths - 获取一棵树从顶点到每个叶节点的路径

1.题目名称 Binary Tree Paths(获取一棵树从顶点到每个叶节点的路径) 2.题目地址 https://leetcode.com/problems/binary-tree-paths/ 3.题目内容 英文:Given a binary tree, return all root-to-leaf paths. 中文:给定一颗二叉树,返回所有的根节点到叶节点的路径 例如:现有一颗二叉树    1  /   2     3    5 所有由根节点到叶节点的路径如下: ["1->2-