翻转二叉树(Invert Binary Tree)
来源:互联网 发布:中国历年cpi数据 编辑:程序博客网 时间:2024/04/28 08:37
最近翻转二叉树算法成为了热点算法,起因就是因为homebrew的作者去google面试的事件,然后业界又出现了一场撕逼大战:对于工作经验和算法能力哪个更重要?不想把战火进一步蔓延,所以在这我也不发表观点了,直接进入正题。
问题描述(直接copy自LeetCode):
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1
这个算法其实不难,在LeetCode中的难易评级也是easy,算法的核心就是递归思想,下面直接上代码,也是笔者在LeetCode中的答案,因为LeetCode不支持oc,只能用js了
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * @param {TreeNode} root * @return {TreeNode} */var invertTree = function(root) { if(root === null){ return null; } root.left = invertTree(root.left); root.right = invertTree(root.right); var tempNode = root.left; root.left = root.right; root.right = tempNode; return root;};
0 0
- 翻转二叉树(Invert Binary Tree)
- Invert Binary Tree(翻转二叉树)
- [Java]Invert Binary Tree翻转二叉树
- LintCode --invert-binary-tree(翻转二叉树)
- 226. Invert Binary Tree 翻转二叉树
- 226. Invert Binary Tree | 翻转二叉树
- LeetCode Invert Binary Tree 翻转二叉树
- 【LeetCode】Invert Binary Tree 翻转二叉树
- 翻转二叉树(Invert Binary Tree)
- 树-Invert Binary Tree(翻转二叉树)
- leetcode系列(11)二叉树翻转 Invert Binary Tree
- 175.Invert Binary Tree-翻转二叉树(容易题)
- LeetCode 226 Invert Binary Tree(翻转二叉树)
- leetcode 226 Invert Binary Tree 翻转二叉树
- [LeetCode] Invert Binary Tree - 二叉树翻转系列问题
- LintCode | Easy | 翻转二叉树 | Invert Binary Tree
- 【LeetCode】226. Invert Binary Tree 二叉树翻转
- Invert Binary Tree(反转二叉树)
- 创业公司都在使用的3款Python库
- 密码学基础知识(九)密钥管理
- poj 1011 Sticks (DFS+剪枝)
- phpstorm8取消自动保存
- TortoiseSVN安装使用
- 翻转二叉树(Invert Binary Tree)
- C/C++校招笔试面试经典题目总结一
- jndi 数据源配置
- 浅析Java虚拟机结构与机制[转]
- Solving “Dynamic Web Module 3.0 requires Java 1.6 or newer” in Maven Projects
- 苹果手机和安卓手机
- Ant 脚本在部署过程中实现系统参数的自动发现
- android实例 listview与sqlite数据绑定
- leetcode 225:Implement Stack using Queues