Invert Binary Tree
来源:互联网 发布:c语言windows窗口程序 编辑:程序博客网 时间:2024/06/18 18:26
c++
/** * 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 == nullptr) return nullptr; if (root->left == nullptr && root->right == nullptr) return root; TreeNode* tmpleft = root->left; TreeNode* tmpright = root->right; root->left = invertTree(tmpright); root->right = invertTree(tmpleft); return root; }};
python
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if not root: return root if not root.left and not root.right: return root root.left, root.right = self.invertTree(root.right),self.invertTree(root.left) return root
0 0
- [leetcode] Invert Binary Tree
- [LeetCode]Invert Binary Tree
- Invert Binary Tree
- leetcode:Invert Binary Tree
- Invert Binary Tree
- LeetCode Invert Binary Tree
- 226Invert Binary Tree
- LeetCode | Invert Binary Tree
- leetcode--Invert Binary Tree
- [leetcode] Invert Binary Tree
- [LeetCode] Invert Binary Tree
- LeetCode Invert Binary Tree
- Invert Binary Tree
- leetcode : Invert Binary Tree
- 226Invert Binary Tree
- Invert Binary Tree
- [LeetCode] Invert Binary Tree
- [Leetcode]Invert Binary Tree
- Python-GraphLab简单应用
- struts2中ajax异步请求action会被自动执行两次
- win7下搭建OpenGL ES 2.0开发环境
- 【Leetcode】Merge Two Sorted Lists
- 启动oracle em命令及操作
- Invert Binary Tree
- C++ 线程池实现原理
- 数据结构_P11
- 项目管理:沟通与沟通计划
- Linked List Cycle
- Spark定制班第21课:Spark Streaming中动态Batch Size深入及RateController解析
- 一些基础的控件
- Java-文本下载
- java写的TCP局域网聊天系统