leetcode 101

101. Symmetric Tree

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

Note:
Bonus points if you could solve it both recursively and iteratively.

判断二叉树是否为平衡二叉树。

递归实现。

代码如下:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSymmetric(TreeNode* root) {
13         if(root == NULL)
14         {
15             return true;
16         }
17         return isSame(root->left, root->right);
18     }
19     bool isSame(TreeNode* left, TreeNode* right)
20     {
21         if(left == NULL && right == NULL)
22         {
23             return true;
24         }
25         else if(left == NULL)
26         {
27             return false;
28         }
29         else if(right == NULL)
30         {
31             return false;
32         }
33
34         if(left->val == right->val && left->left == NULL && left->right && right->right == NULL && right->left == NULL)
35         {
36             return true;
37         }
38         else if(left->val == right->val)
39         {
40             return isSame(left->left, right->right) && isSame(left->right, right->left);
41         }
42         else
43         {
44             return false;
45         }
46
47     }
48 };
时间: 2024-07-31 05:51:06

leetcode 101的相关文章

LeetCode 101 Symmetric Tree (C)

题目: 101. Symmetric Tree QuestionEditorial Solution My Submissions Total Accepted: 135232 Total Submissions: 375037 Difficulty: Easy Contributors: Admin Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

leetCode 101. Symmetric Tree 对称树

101. Symmetric Tree 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]

Leetcode 101 Symmetric Tree 二叉树

判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert Binary Tree,然后用Same Tree比较左右子树 而我的做法是改下Same Tree的函数,改动的是第27行 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNo

[LeetCode]101. Trapping Rain Water收集雨水

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a

LeetCode 101. Symmetric Tree (对称树)

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 Note:Bonus

Java for LeetCode 101 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 解题思路: 重载一个isSymmetric判断两棵树是否对称即可,JAVA实现如下: public

Java [Leetcode 101]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 a bina

leetcode || 101、Symmetric Tree

problem: 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

[C++]LeetCode: 101 Binary Tree Zigzag Level Order Traversal

题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its