这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » STM32 » binary-tree-level-order-traversal

共2条 1/1 1 跳转至

binary-tree-level-order-traversal

高工
2018-01-18 20:26:09     打赏

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.


OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
  / \
 2   3
    /
   4
    \
     5
The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".
题意:判断二叉树是否是镜像二叉树[cpp] view plain copy
  1. class Solution {  

  2. public:  

  3.     bool isSymmetirc(TreeNode* root)  

  4.     {  

  5.         if (root == NULL)  

  6.         {  

  7.             return true;  

  8.         }  

  9.   

  10.         return check(root->left, root->right);  

  11.     }  

  12.   

  13.     bool check(TreeNode* leftNode, TreeNode* rightNode)  

  14.     {  

  15.         if (leftNode==NULL && rightNode==NULL)  

  16.         {  

  17.             return true;  

  18.         }  

  19.   

  20.         if ((leftNode!=NULL && rightNode==NULL)  

  21.             || (leftNode==NULL && rightNode!=NULL))  

  22.         {  

  23.             return false;  

  24.         }  

  25.           

  26.         if (leftNode->val!=rightNode->val)  

  27.         {  

  28.             return false;  

  29.         }  

  30.   

  31.         return check(leftNode->left, rightNode->right) && check(leftNode->right, rightNode->left);  

  32.     }  

  33. }; 




专家
2018-01-19 09:15:28     打赏
2楼

不错的资料分享。


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]