Symmetric Tree问题及解法
来源:互联网 发布:环太平洋配乐 知乎 编辑:程序博客网 时间:2024/06/08 03:40
问题描述:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
示例:
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1 / \ 2 2 / \ / \3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1 / \ 2 2 \ \ 3 3
问题分析:
本题可以看作SameTree的一个变体,详细分析可参考我的Same Tree问题及解法
下面是详细代码:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isSymmetric(TreeNode* root) { return isSameTree(root,root); } bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL && q == NULL) return true; else if(p == NULL) return false; else if(q == NULL) return false; if(p->val == q->val) { return isSameTree(p->left,q->right) && isSameTree(p->right,q->left); } else return false; } };
有疑问欢迎交流~~
0 0
- Symmetric Tree问题及解法
- Same Tree问题及解法
- LeetCode 101. Symmetric Tree 递归解法
- Balanced Binary Tree问题及解法
- Diameter of Binary Tree问题及解法
- Binary Tree Tilt问题及解法
- Subtree of Another Tree问题及解法
- Binary Tree Inorder Traversal问题及解法
- Validate Binary Search Tree问题及解法
- Binary Tree Preorder Traversal问题及解法
- Binary Search Tree Iterator问题及解法
- Maximum Binary Tree问题及解法
- Print Binary Tree问题及解法
- Leetcode Symmetric Tree 递归和非递归解法
- LeetCode题解:Symmetric Tree(有4种解法)
- Maximum Depth of Binary Tree问题及解法
- Binary Tree Level Order Traversal II问题及解法
- Convert Sorted Array to Binary Search Tree问题及解法
- git revert 和reset的区别
- Tensorflow一些常用基本概念与函数2
- IBatis
- C++ ->的作用
- Tensorflow一些常用基本概念与函数3
- Symmetric Tree问题及解法
- mate标签
- 浅谈自定义View之自定义布局FlowLayout
- Java垃圾回收学习笔记
- JAVA POI导入技术之EXCEL模板中制作下拉列表
- 运算符重载的实现代码
- MySQL中的存储引擎讲解(InnoDB,MyISAM,Memory等各存储引擎对比)
- codeforces 785 E. Anton and Permutation(分块)
- 使用Python实现每日一句英语发送到手机