【leetcode】Symmetric Tree

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

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.

对称的递归表达式:

testMirror(leftNode->left,rightNode->right)&&testMirror(leftNode->right,rightNode->left);

leftNode->left->val==rightNode->right->val

leftNode->right->val==rightNode->left->val

 1 class Solution {
 2
 3 public:
 4
 5     bool isSymmetric(TreeNode *root) {
 6
 7         if(root==NULL)
 8
 9         {
10
11             return true;
12
13         }
14
15         return testMirror(root->left,root->right);
16
17
18
19     }
20
21
22
23     bool testMirror(TreeNode *leftNode,TreeNode *rightNode)
24
25     {
26
27
28
29         if(leftNode==NULL&&rightNode==NULL)
30
31             return true;
32
33         if(leftNode!=NULL&&rightNode==NULL||leftNode==NULL&&rightNode!=NULL)
34
35             return false;
36
37         if(leftNode->val!=rightNode->val)
38
39             return false;
40
41
42
43         return testMirror(leftNode->left,rightNode->right)&&testMirror(leftNode->right,rightNode->left);
44
45
46
47
48
49     }
50
51 };
时间: 2024-10-26 19:50:48

【leetcode】Symmetric Tree的相关文章

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree <span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; *

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【LeetCode】Symmetric Tree (2 solutions)

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 Note:Bonus points if you could sol

【Leetcode】Binary Tree Postorder Traversal

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 solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

【Leetcode】Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 思路:使用

【Leetcode】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

【LeetCode】Binary Tree Right Side View 解题报告

[题目] Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3

【LeetCode】Binary Tree Maximum Path Sum 解题报告

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [解析] 题意:在二叉树中找一条路径,使得该路径的和最大.该路径可以从二叉树任何结点开始,也可以到任何结点结束. 思路:递归求一条经过root的最大路径,这条路径可能是:

【LeetCode】 Same Tree

题目:Same Tree /** * LeetCode Same Tree * 题目:判断两棵树是否是完全相同的 * 思路:完全相同:1.结构相同: * 2.值相同. * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ package javaTrain; public c