• 剑指offer-二叉树


    二叉树的下一个结点

    给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。


     

    解题思路:中序遍历:左根右

    二叉树结点:

      为空

      为左子树,其下个结点为其父节点

      为右子树,其下个节点为其父节点的父节点。。。直到找到父节点


     

    
    

    对称二叉树

    请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

     1 /*
     2 struct TreeNode {
     3     int val;
     4     struct TreeNode *left;
     5     struct TreeNode *right;
     6     TreeNode(int x) :
     7             val(x), left(NULL), right(NULL) {
     8     }
     9 };
    10 */
    11 class Solution {
    12 public:
    13     bool isSymmetrical(TreeNode* pRoot)
    14     {
    15         if(pRoot==nullptr)
    16             return 1;
    17         
    18         return(Symmetrical(pRoot->left,pRoot->right));
    19     
    20     }
    21     
    22     bool Symmetrical(TreeNode* pRight, TreeNode* pLeft)
    23     {
    24         if((pRight==nullptr)&&(pLeft==nullptr)) return 1;
    25         
    26         if((pRight!=nullptr)&&(pLeft!=nullptr)) 
    27             return((pRight->val==pLeft->val)&&Symmetrical(pLeft->left,pRight->right)&&Symmetrical(pLeft->right,pRight->left));
    28         
    29             
    30         return 0;
    31     }
    32 
    33 };

     

  • 相关阅读:
    python中的编码与解码
    python中的迭代器
    python中高阶函数与装饰器(3)
    python中高阶函数与装饰器(2)
    python中高阶函数与装饰器
    python中几种常用的数据类型
    python 启航
    Python [习题] 字典排序
    SQL-TSQL
    SQL基础语句
  • 原文地址:https://www.cnblogs.com/lemon333333/p/10235199.html
Copyright © 2020-2023  润新知