LeetCode 226. Invert Binary Tree 题解(C++)
来源:互联网 发布:网络测试命令 编辑:程序博客网 时间:2024/06/06 11:42
LeetCode 226. Invert Binary Tree 题解(C++)
题目描述
- Invert a binary tree.
示例
Input:
4
/ \
2 7
/ \ / \
1 3 6 9Output:
4
/ \
7 2
/ \ / \
9 6 3 1
思路
- 用递归实现二叉树的翻转。递归出口为当结点为空时,返回NULL。否则对左右子树进行递归翻转,之后对翻转完成的左右子树的位置进行交换即可。
代码
/** * 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; } root->left = invertTree(root->left); root->right = invertTree(root->right); TreeNode *temp = root->left; root->left = root->right; root->right = temp; return root; }};
0 0
- LeetCode 226. Invert Binary Tree 题解(C++)
- leetcode 226. Invert Binary Tree 题解
- Leetcode题解 226. Invert Binary Tree
- LeetCode Algorithms 226. Invert Binary Tree 题解
- LeetCode题解–226. Invert Binary Tree
- LeetCode题解:Invert Binary Tree
- LeetCode题解:Invert Binary Tree
- LeetCode 226. Invert Binary Tree C语言
- LeetCode 226. Invert Binary Tree (c++)
- LeetCode 226. Invert Binary Tree 题解 —— Java
- LeetCode 题解(192) : Invert Binary Tree
- LeetCode题解-226-Invert Binary Tree
- LeetCode 226 Invert Binary Tree题解
- LeetCode 226. Invert Binary Tree(Python)
- LeetCode 226. Invert Binary Tree
- [leetcode] 226.Invert Binary Tree
- [leetcode] 226. Invert Binary Tree
- 【LeetCode】226. Invert Binary Tree
- 使用HDFS的Java API
- Activity 的生命周期
- jq e.target与e.currentTarget的异同
- Ubuntu 14.04 安装Caffe(CPU) 且支持 PYTHON接口
- lua闭包全面解析
- LeetCode 226. Invert Binary Tree 题解(C++)
- Linux系统用户/文件&用户和组命令 练习案例
- MS CRM 2016 certificate备考摘要(1) - 2016的新功能from MSDN
- HDU_2955 动态规划
- INV-MO-The material sourcing Process failed to create picking suggestions
- 递归与迭代的区别
- 健康的体质
- Codeforces723E - One-Way Reform(Euler回路)
- hdu 5927 LCA的dfs实现(预处理)+无向图转有根树