[leetcode]Symmetric Tree
LeetCode Tree
2023-09-14 09:10:19 时间
问题叙述性说明:
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 recursively and iteratively.
confused what "{1,#,2,3}"
means?
> read more on how binary tree is serialized on OJ.
代码:
递归方法
public class Symmetric_Tree { //java public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } //recursively public boolean isSymmetric(TreeNode root) { if(root == null) return true; return isSymmetricTree(root.left, root.right); } public boolean isSymmetricTree(TreeNode left, TreeNode right){ if(left == null && right == null) return true; if(left == null || right == null ||left.val != right.val) return false; boolean lr = isSymmetricTree(left.left, right.right); boolean rl = isSymmetricTree(left.right, right.left); return lr && rl; } }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
相关文章
- ☆打卡算法☆LeetCode 210. 课程表 II 算法解析
- ☆打卡算法☆LeetCode 215. 数组中的第K个最大元素 算法解析
- 几道入门的回溯题 | LeetCode
- LeetCode笔记:Weekly Contest 302
- LeetCode周赛288,高难度酣畅淋漓的比赛
- leetcode-76最小覆盖子串(双指针)
- leetcode-103二叉树的锯齿形层序遍历「建议收藏」
- leetcode-124. 二叉树中的最大路径和(树形dp)
- leetcode 647. 回文子串 js实现
- LeetCode 121. 买卖股票的最佳时机
- leetcode 22 括号生成 js 实现
- 前端工程师leetcode算法面试必备-二叉树的构造和遍历
- Binary Tree Postorder Traversal — LeetCode
- leetcode 链表初探 21. merge two sorted lists
- LeetCode-448-找到所有数组中消失的数字
- Linux系统下使用Tree命令查看目录结构(tree命令linux)