7.Invert Binary Tree
来源:互联网 发布:网页传奇游戏源码 编辑:程序博客网 时间:2024/06/04 19:48
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 awhiteboard so fuck off.
分析可知:这道题目是要求实现二叉树的反转。
所以在实现的过程中我采用的是递归的做法。
public TreeNode invertTree(TreeNode root) { if (root == null ) {return root;}TreeNode temp = root.left;root.left = root.right;root.right = temp;invertTree(root.left);invertTree(root.right);return root; }
public TreeNode invertTree(TreeNode root) {if (root == null || root.left == null && root.right == null) {/*在这个地方加上判断根节点的左右孩子是否为空,则少去了对整体二叉树中每个叶子节点再进行递归*/return root;}TreeNode temp = root.left;root.left = root.right;root.right = temp;invertTree(root.left);invertTree(root.right);return root;}之前做的Maximum Depth of Binary Tree题目和这Invert Binary Tree题目、Same Tree题目都是运用了递归的思想。
0 0
- 7.Invert Binary Tree
- [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
- 解决XCode7 老是提示 意外退出 的问题
- 【iOS-pod】配置CocoaPods三个要注意的地方
- 完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
- msc和gcc编译器的内置函数(Compiler Intrinsics/Built-in Functions)
- UISegmentControl
- 7.Invert Binary Tree
- 一些关于C++ 内存管理的比较好的文章,代码
- Centos 7 使用Apache部署Redmine。
- Linux Kernel 2.6.3
- HDOJ 题目3265 Posters(线段树+扫描线)
- PHP开发时路由中GET方法和POST方法的区别
- Python 常用处理
- x86_64 函数调用约定
- MySQL学习笔记9:连接查询