leetCode(14):Invert Binary Tree and Same Tree
2015-06-21 11:48
344 查看
反转二叉树:
判断是否是相同二叉树:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root==NULL) return NULL; if(root->left==NULL && root->right==NULL) return root; TreeNode* tmpNode=root->right; root->right=root->left; root->left=tmpNode; if(root->left) { root->left=invertTree(root->left); } if(root->right) { root->right=invertTree(root->right); } return root; } };
判断是否是相同二叉树:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p==NULL && q==NULL) return true; if(p==NULL || q==NULL) return false; if(p->val==q->val) { return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); } else { return false; } } };
相关文章推荐
- leetCode(14):Invert Binary Tree and Same Tree 分类: leetCode 2015-06-21 11:48 170人阅读 评论(0) 收藏
- slick与mysql
- 增强型for循环的各个参数
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第十三课:Cocos2D-X内存管理4-6
- activity-alias使用
- paperclip用lambda来动态定义 crop/resize 的图片大小
- 日语学习之沪江N3基础 20150621 -3
- Windows Phone 获取本地IP地址
- 教你21天学会C++ (有图有真相)
- leetcode--Implement Stack using Queues
- 渣渣ACM日记——278-排队(NYOJ)
- ubuntu日常使用常用快捷键(待补充)
- 长轮询解决方案
- 黑马程序员——学习日记之--Java网络编程小结
- 数组结对开发
- redis翻译_redis lua脚本
- 多态机制
- .pb.h:9:42: fatal error: google/protobuf/stubs/common.h: No such file or directory
- java基础之 异常
- Windows 10 Insider预览版免费升级正式版系统需是主系统