LintCode - 175.翻转二叉树
2016-12-23 22:47
316 查看
翻转一棵二叉树
您在真实的面试中是否遇到过这个题?
Yes
样例
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
void invertBinaryTree(TreeNode *root) {
// write your code here
if( root == NULL ) return;
TreeNode* node = root->left;
root->left = root->right;
root->right = node;
invertBinaryTree( root->left );
invertBinaryTree( root->right );
}
};
您在真实的面试中是否遇到过这个题?
Yes
样例
1 1 / \ / \ 2 3 => 3 2 / \ 4 4
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
void invertBinaryTree(TreeNode *root) {
// write your code here
if( root == NULL ) return;
TreeNode* node = root->left;
root->left = root->right;
root->right = node;
invertBinaryTree( root->left );
invertBinaryTree( root->right );
}
};
相关文章推荐
- lintcode翻转二叉树
- LintCode(Flatten Binary Tree to Linked List)翻转二叉树为链表 的变体
- lintcode,翻转二叉树
- 算法题:lintcode#175 翻转二叉树(java)
- lintcode——翻转二叉树
- LintCode:二叉树的最小深度
- 二叉树中路径和为某整数的所有路径 lint code by python
- LintCode-二叉树的最大深度
- lintcode&九章算法——No.70 二叉树的层次遍历(二) ? 待解决
- Lintcode:翻转链表
- 日常练习:lintcode 53. 翻转字符串
- LintCode2016年8月22日算法比赛----克隆二叉树
- LintCode Reserve Integer 翻转整数
- Lintcode 376. 二叉树的路径和
- LintCode之35 翻转链表 标签: 链表
- lintcode--二叉树的后序遍历
- lintcode binary-tree-paths 二叉树的所有路径
- lintcode minimum-depth-of-binary-tree 二叉树最小深度
- LintCode-35.翻转链表
- LintCode:翻转链表 II