LeetCode -- Invert Binary Tree
2015-10-30 23:24
323 查看
题目描述:
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
就是把一棵二叉树进行反转。
思路:
本题的实现比较直接:后续遍历,交换节点。
实现代码:
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
就是把一棵二叉树进行反转。
思路:
本题的实现比较直接:后续遍历,交换节点。
实现代码:
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode InvertTree(TreeNode root) { Travel(ref root); return root; } private void Travel(ref TreeNode current) { if(current == null || current.left == null && current.right == null){ return; } Travel(ref current.left); Travel(ref current.right); Swap(ref current); } private void Swap(ref TreeNode n) { var tmp = n.left; n.left = n.right; n.right = tmp; } }
相关文章推荐
- hdu 4648 Magic Pen 6(贪心)
- 第二波Android的心得即将袭来~
- Android中Activity的生命周期
- LeetCode -- House Robber II
- MySQL运行两个或多个实例
- Maven内置隐式变量
- LeetCode -- First Bad Version
- Android开发之旅:android架构
- java冒泡排序
- UIAlterController 的使用
- 1045. 快速排序(25)
- LeetCode -- Expression Add Operators
- 线上服务由于缓冲区溢出造成的惨案
- hdu 4647 Another Graph Game(高效)
- gcc中的-w -W和-Wall选项
- Uva1593——Alignment of Code
- LeetCode -- Combination Sum III
- 触摸事件分发机制详解
- 关于VFS文件系统中的superblock、inode、d_entry和file数据结构
- ANDROID开发环境