【Leetcode】Invert Binary Tree
来源:互联网 发布:唯一网络哪里人王宇杰 编辑:程序博客网 时间:2024/06/06 22:45
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.
This is a very famous conversation and the answer is actually quite simply if you recognize the essence of this problem.
Basically, we switched every left node with its right and so does its child. Another thought is you could traverse all nodes and put them in a stack. Then do the same kind of traverse again and pop node from the stack.
I wrote the first kind of method using recursive. I simply switch the left with the right and go on and on.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode invertTree(TreeNode root) { if(root==null) return root; TreeNode tmp = invertTree(root.right); root.right = invertTree(root.left); root.left = tmp; return root; }}
0 0
- [leetcode] Invert Binary Tree
- [LeetCode]Invert Binary Tree
- leetcode:Invert Binary Tree
- LeetCode Invert Binary Tree
- LeetCode | Invert Binary Tree
- leetcode--Invert Binary Tree
- [leetcode] Invert Binary Tree
- [LeetCode] Invert Binary Tree
- LeetCode Invert Binary Tree
- leetcode : Invert Binary Tree
- [LeetCode] Invert Binary Tree
- [Leetcode]Invert Binary Tree
- LeetCode: Invert Binary Tree
- LeetCode Invert Binary Tree
- leetcode: Invert Binary Tree
- LeetCode Invert Binary Tree
- LeetCode || Invert Binary Tree
- LeetCode:Invert Binary Tree
- 删除Target 重名的问题...
- 2016春《嵌入式系统及应用》紧张筹备中
- 何时使用section标签
- HDU2222: Keywords Search AC自动机
- 加载slidingmenu出错,找不到android-support-v4
- 【Leetcode】Invert Binary Tree
- 缩短浏览器上页面渲染
- OSX 10.11 系统下安装mcrypt插件
- 回调函数...
- FragmentTabHost使用方法
- 深入理解Tomcat 6和Tomcat7的区别
- Mac 下配置 Latex 支持中文
- 从头认识java-17.1 多线程
- Local模式下开发第一个Spark程序并运行于集群环境