博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Same Tree-相同树
阅读量:4108 次
发布时间:2019-05-25

本文共 714 字,大约阅读时间需要 2 分钟。

问题:

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.

解答:

递归判断,两棵树相同,当且仅当值相同并且左右子树相同。

/** * 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(p != NULL && q != NULL)            return (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);        else if(p == NULL && q == NULL)            return true;        else            return false;    }};

转载地址:http://eztsi.baihongyu.com/

你可能感兴趣的文章
GCC出现warning: integer constant is too large for 'long' type"
查看>>
winAVR 全局变量volatile
查看>>
OP AMP - 单电源对运算放大器的影响
查看>>
如何正确选择AD/DA器件
查看>>
IAP ISP 区别
查看>>
STM32 注意的地方
查看>>
PCB Layout 中的直角走线、差分走线和蛇形线
查看>>
layout中蛇形线和差分线的使用
查看>>
ppm/℃是什么单位?什么意思?
查看>>
Java通过引用js脚本引擎实现精确计算
查看>>
面向对象-关于对象
查看>>
单向链表 实现(非线程安全)
查看>>
全国省市区信息,mysql数据库记录
查看>>
1.0 Linux文件系统
查看>>
2.0 Linux进程
查看>>
2.1 Linux 启动新进程
查看>>
2.2.1 进程管理,以及父子进程共享同一个文件资源时,文件的‘读写位置’会相互影响
查看>>
Linux 信号常量表
查看>>
Linux 错误码(error code)列表(头文件 ‘errno.h’)
查看>>
Linux 编程中的错误处理
查看>>