226.leetcode Invert Binary Tree(easy)[二叉树 递归]
来源:互联网 发布:家用除尘设备知乎 编辑:程序博客网 时间:2024/04/20 00:54
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
题目的含义是得到左子树与右子树交换的树,那么按照树的递归方式分别交换每个节点的左右子树即可。
/** * 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->left = invertTree(root->left); if(root->right != NULL) root->right = invertTree(root->right); TreeNode *temp = root->left; root->left = root->right; root->right = temp; return root; }};
0 0
- 226.leetcode Invert Binary Tree(easy)[二叉树 递归]
- 【leetcode】【Easy】【226. Invert Binary Tree】【tree】
- Leetcode 226. Invert Binary Tree (Easy) (cpp)
- LeetCode 226. Invert Binary Tree (Easy)
- leetcode-226. Invert Binary Tree 二叉树、递归,交换两个变量的值
- leetcode 226. Invert Binary Tree(C语言,二叉树,递归)54
- 226. Invert Binary Tree -- 二叉树、递归、交换、栈
- [LeetCode][二叉树]Invert Binary Tree
- LeetCode Invert Binary Tree 翻转二叉树
- 【LeetCode】Invert Binary Tree 翻转二叉树
- leetcode 226. Invert Binary Tree(二叉树的镜像)
- LeetCode 226. Invert Binary Tree(反转二叉树)
- 【LeetCode】226. Invert Binary Tree 二叉树翻转
- LeetCode OJ-226. Invert Binary Tree(反转二叉搜索树)
- [LeetCode]226. Invert Binary Tree(反转二叉树)
- [LeetCode]226. Invert Binary Tree--反转二叉树
- 226. Invert Binary Tree (Easy)
- Leetcode 226: Invert Binary Tree(二叉树反转 递归、非递归实现)
- 在安霸s2lm上wifi定频测试
- getattr(),setattr(),delattr(),hasattr()
- 【机房收费】--上下机流程
- 多线程:继承Thread和实现Runnable的区别
- 杭电2845
- 226.leetcode Invert Binary Tree(easy)[二叉树 递归]
- android动画Animation
- 2016年8月5号
- LeetCode Factorial Trailing Zeroes数学方法详解
- hdu 5748 Bellovin(BestCoder Round #84——最长递增子序列)
- 如何拿到优酷的视频真实地址
- 数据结构实验之串一:KMP简单应用
- 桥接模式和适配器模式的区别
- 行状元素和块状元素