leetCode(14):Invert Binary Tree and Same Tree
来源:互联网 发布:淘宝店铺怎么上架宝贝 编辑:程序博客网 时间:2024/05/18 18:42
反转二叉树:
/** * 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; } }};
0 0
- leetCode(14):Invert Binary Tree and Same Tree
- [leetcode] Invert Binary Tree
- [LeetCode]Invert Binary Tree
- leetcode:Invert Binary Tree
- LeetCode Invert Binary Tree
- LeetCode | Invert Binary Tree
- leetcode--Invert Binary Tree
- [leetcode] Invert Binary Tree
- [LeetCode] Invert Binary Tree
- LeetCode Invert Binary Tree
- leetcode : Invert Binary Tree
- [LeetCode] Invert Binary Tree
- [Leetcode]Invert Binary Tree
- LeetCode: Invert Binary Tree
- LeetCode Invert Binary Tree
- leetcode: Invert Binary Tree
- LeetCode Invert Binary Tree
- LeetCode || Invert Binary Tree
- 渣渣ACM日记——278-排队(NYOJ)
- 长轮询解决方案
- 毕业季致青春
- leetcode--Implement Stack using Queues
- Netty权威指南之TCP粘包和拆包
- leetCode(14):Invert Binary Tree and Same Tree
- Windows Phone 获取本地IP地址
- slick与mysql
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第十三课:Cocos2D-X内存管理4-6
- 增强型for循环的各个参数
- POJ 1611 The Suspects(并查集)
- leetCode(15):Symmetric Tree
- Linux Bash 快捷键大全
- solr/solrj按id进行索引更新