leetcode——Same Tree (二叉树的递归-先序遍历)

Same Tree

Total Accepted: 56263 Total Submissions: 133912My Submissions

Question Solution

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

Have you met this question in a real interview?

Yes

No

Discuss

#include<iostream>
#include<vector>
using namespace std;

struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

//这样做可以AC,但是有点问题,将两个树都采用先序遍历,将遍历结果都存入到
//vector中,然后比较两个vector,而在这中间有个问题就是在空的结点出我存的是‘#‘,而
//转化为asii码,但这有点问题的
void first_search(TreeNode* root,vector<int>& vec)
{
	if(root!=NULL)
		vec.push_back(root->val);
	else
	{vec.push_back(‘#‘);return;}
	first_search(root->left,vec);
	first_search(root->right,vec);
	return;
}
bool isSameTree(TreeNode *p, TreeNode *q)
{
	vector<int> vec1,vec2;
	first_search(p,vec1);
	first_search(q,vec2);
	if(vec1==vec2)
		return 1;
	else
		return 0;
}

int main()
{

}

  

时间: 2024-08-25 06:00:31

leetcode——Same Tree (二叉树的递归-先序遍历)的相关文章

【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

[145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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 soluti

完全二叉树的链式存储结构的转化 &amp; 非递归中序遍历二叉树

1 /* 2 * 二叉树 3 * 4 * (将完全二叉树的数组形式改为链表形式) 5 * 6 * 1 7 * 2 3 8 * 4 5 6 7 9 * 8 10 * 11 */ 12 #include <iostream> 13 #define MAX 10 14 using namespace std; 15 16 typedef struct btnode{ 17 int data; 18 struct btnode * lchild; 19 struct btnode * rchild;

非递归后序遍历二叉树(1)

1 void postOrder3(BinTree *root) //非递归后序遍历 2 { 3 stack<BinTree*> s; 4 BinTree *cur; //当前结点 5 BinTree *pre=NULL; //前一次访问的结点 6 s.push(root); 7 while(!s.empty()) 8 { 9 cur=s.top(); 10 if((cur->lchild==NULL&&cur->rchild==NULL)|| 11 (pre!=N

递归降序遍历目录层次结构

在学习APUE第4章时候,里面编写了一段递归顺序遍历目录层次的结构的代码,该代码实现了递归访问目录.但是该代码并没有显示降序的方式显示目录树. 因此,我讲代码稍微修改,使其能够按照tree命令的方式显示,同时也统计了各个文件的数量. #include<iostream> #include<cstring> #include<vector> #include<dirent.h> #include<unistd.h> #include<algo

七:重建二叉树(根据先序遍历(或者后序遍历)和中序遍历重建二叉树)

对于一颗二叉树,可以根据先序遍历(或者后序遍历)和中序遍历(树中不含重复的数字)重新还原出二叉树. 解析: 1. 先序遍历序列的第一个元素必定是根节点,可以由此获取二叉树的根节点. 2. 根据根节点,在中序遍历序列中查找该节点,由中序遍历的性质可知,中序遍历中该根节点左边的序列必定在根节点的左子树中,而根节点右边的序列必定在右子树中.由此可以知道先序遍历中左子树以及右子树的起止位置. 3. 找到了左右子树前序遍历和中序遍历再用同样的方法分别构建左右子树,典型的递归思想. 代码如下: Binary

算法题——二叉树结点的中序遍历的后继结点

题目:给出二叉树的一个结点,返回它中序遍历顺序的下一个结点. 思路: 如果有指向父亲的结点,则: 如果当前结点有右儿子,或者当前结点是根结点,则后继结点为右子树的最左叶节点: 否则,如果当前结点是父结点的左儿子,则后继结点就是父结点:(其实是第三种情况的一个特例,即自己是第0代祖先,返回第一代祖先) 否则,向上遍历,直到n-1代祖先是n代祖先的左儿子,则后继结点为n代祖先:或者遍历到根节点后未找到符合的n代结点,则该结点为中序遍历的最后结点,没有后继. 时间复杂度为树的高度O(lgN). 代码:

递归降序遍历目录层次结构,并按文件类型计数

本程序使用了一些对目录进行操作的函数编写了一个遍历文件层次结构的程序,最后对各种类型的文件计数.这个程序只有一个参数,它说明起点路径名,从该点开始递归降序遍历文件层次结构.其中还用到了一个为路径名动态分配存储区的函数path_alloc. // ftw.c // 2015-08-18 Lucifer Zhang // Recursively descend a directory hierarchy, counting file types #include "apue.h" #inc

写了一个二叉树构造及中序遍历函数

本题就是测试读入数据的速度的. 如果有大量的数据读入,使用cin是很慢的. 那么使用scanf那么会快很多,但是如果数据量更大的话那么就还是不够快了. 所以这里使用fread. 首先开一个buffer,然后使用fread大块大块地读入数据就可以非常快地读入了. 题目如下: Input The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive

LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively?中序遍历二叉树,递归遍历当然很容易,题目还要求不用递归,下面给出两种方法: 递归: 1 /**