same-tree/symmetric-tree
来源:互联网 发布:网络监控数据线连接 编辑:程序博客网 时间:2024/06/01 14:44
题目一: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.
/** * 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 && !q) return true; if(p && !q) return false; if(!p && q) return false; if(p->val ==q->val) return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); else return false; }};
题目二: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
/** * 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 isSymmetric(TreeNode *root) { if(root==NULL) return true; return ismirror(root->left,root->right); } bool ismirror(TreeNode *p,TreeNode *q) { if(!p && !q) return true; if(!p && q) return false; if(p && !q) return false; if(p->val ==q->val) return ismirror(p->left,q->right) && ismirror(p->right,q->left); else return false; }};
阅读全文
0 0
- Same Tree & Symmetric Tree
- same-tree/symmetric-tree
- [LeetCode] Same Tree、Symmetric Tree
- LeetCode-Same Tree & Symmetric Tree
- LeetCode-- Same Tree & Symmetric Tree
- LeetCode 100. Same Tree && 101. Symmetric Tree
- 【leetcode】Same Tree and Symmetric Tree-----Java
- 100. Same Tree 101. Symmetric Tree
- 【LeetCode从零单排】No100 Same Tree && No101 Symmetric Tree
- [Java语言] Same Tree Symmetric Tree 相同树 对称树
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- python学习——random模块
- perl中system函数和反引号` `使用区别
- [MVVM]01-MVVMLight安装
- 左神指南第一章_栈和队列
- 决策树模型
- same-tree/symmetric-tree
- 部署Jenkins
- K3 CLOUD 插件的学习
- USACO 1.4 Combination Lock
- POJ
- 插入排序java
- keccak算法及实现
- malloc函数的学习
- linux鸟哥视频学习笔记03