【Leetcode-easy-226】Invert Binary Tree
来源:互联网 发布:app软件测试视频教程 编辑:程序博客网 时间:2024/04/20 11:11
【Leetcode-easy-226】Invert Binary Tree
题目
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9
to
4 / \ 7 2 / \ / \9 6 3 1
Trivia:
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.
思路
递归
和剑指offer 面试题19 二叉树的镜像基本一样。
程序
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode invertTree(TreeNode root) { mirrorTree(root); return root; } private void mirrorTree(TreeNode root) { if (root == null) return; TreeNode tmp = root.left; root.left = root.right; root.right = tmp; mirrorTree(root.left); mirrorTree(root.right); }}
声明:题目来自Leetcode。
阅读全文
1 0
- [Leetcode 226, Easy] Invert Binary Tree
- <LeetCode><Easy>226Invert a binary tree
- LeetCode #226 - Invert Binary Tree - Easy
- 【Leetcode-easy-226】Invert Binary Tree
- 【leetcode】【Easy】【226. Invert Binary Tree】【tree】
- leetcode 226 Invert Binary Tree(难易度:Easy)
- Leetcode 226. Invert Binary Tree (Easy) (cpp)
- LeetCode 226. Invert Binary Tree (Easy)
- Invert Binary Tree(easy)
- Easy 226题 Invert Binary Tree
- Leetcode[226]-Invert Binary Tree
- LeetCode 226 Invert Binary Tree
- Invert Binary Tree - LeetCode 226
- leetcode[226]:Invert Binary Tree
- leetcode 226: Invert Binary Tree
- Leetcode 226 Invert Binary Tree
- leetcode-226-Invert Binary Tree
- leetcode 226 :Invert Binary Tree
- java集合之ArrayList源码解读 帮助大家自己动手写一个ArrayList
- Jzoj4710 Value
- 二十一世纪的计算大会:人工智能·未来之路
- 《深度学习——Andrew Ng》第一课第四周编程作业
- js获取最大整数的方法
- 【Leetcode-easy-226】Invert Binary Tree
- PAT --- 1009. 说反话 (20)
- oracle数据库
- 面向对象的六大原则
- 《Spring Boot in Action》【7. Actuator】
- 线性表之顺序表与单链表的区别与优缺点
- 原生JavaScript+Ajax无刷新加载另一个页面的一部分内容
- 阿里面试回来,想和Java程序员谈一谈
- NAS数据迁移初探