算法作业_38(2017.6.20第十八周)
来源:互联网 发布:游戏美工培训学费 编辑:程序博客网 时间:2024/05/27 20:51
226. Invert Binary Tree
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1100. Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
解析:二叉树的反转和判断问题,简单的递归问题:
Invert Binary Tree:
/** * 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; invertTree(root->left); invertTree(root->right); swap(root->left,root->right); return root ; }};
Same Tree:
/** * 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) || (p==NULL && q!=NULL) || (p->val!=q->val)) return false; return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); }};
阅读全文
0 0
- 算法作业_38(2017.6.20第十八周)
- 算法作业_37(2017.6.19第十八周)
- 算法作业_39(2017.6.21第十八周)
- 算法作业_40(2017.6.22第十八周)
- 算法作业_41(2017.6.24第十八周)
- 算法第十八周作业01
- 第十八周作业 1.0
- 算法设计与结构基础作业第十八周
- 算法课第十八周作业 | Course Schedule II
- 算法课第十八周Same Tree
- leetcode第十八周解题总结-贪心算法
- 第十八周
- 第十八周
- 第十八周算法分析与设计:Add Two Numbers
- 算法作业_25(2017.6.1第十五周)
- 算法作业_26(2017.6.6第十六周)
- 算法作业_27(2017.6.8第十六周)
- 算法作业_28(2017.6.8第十六周)
- 关于脑裂
- Android Studio APK打包
- APK瘦身
- binder学习概述篇和路线图
- JS函数
- 算法作业_38(2017.6.20第十八周)
- Swift15-闭包
- Linux 信号(二)信号集
- ###django 不依赖工程执行orm操作
- github常见操作和常见错误!错误提示:fatal: remote origin already exists.
- android选择城市三级联动
- JAVA中的数据存储(堆及堆栈)
- ROC和AUC介绍以及如何计算AUC F
- yii中cookie的使用