[Lintcode] #175 翻转二叉树
来源:互联网 发布:windows自动修复 编辑:程序博客网 时间:2024/06/05 22:45
翻转一棵二叉树
样例
1 1 / \ / \2 3 => 3 2 / \ 4 4
非递归:
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /* * @param root: a TreeNode, the root of the binary tree * @return: nothing */ public void invertBinaryTree(TreeNode root) { // write your code here if (root == null)return;Queue<TreeNode> data = new LinkedList<>();data.add(root);while (!data.isEmpty()) {TreeNode cur = data.poll();TreeNode temp = cur.left;cur.left = cur.right;cur.right = temp;if (cur.left != null)data.add(cur.left);if (cur.right != null)data.add(cur.right);}return; }}
递归:
public class _175 {public void invertBinaryTree(TreeNode root) {if (root == null)return;TreeNode temp = root.left;root.left = root.right;root.right = temp;invertBinaryTree(root.left);invertBinaryTree(root.right);}class TreeNode {public int val;public TreeNode left, right;public TreeNode(int val) {this.val = val;this.left = this.right = null;}}}
阅读全文
0 0
- lintcode 175 翻转二叉树
- [Lintcode] #175 翻转二叉树
- LintCode 翻转二叉树
- lintcode ----翻转二叉树
- lintcode,翻转二叉树
- LintCode-翻转二叉树
- LintCode 翻转二叉树
- 翻转二叉树-LintCode
- lintcode 翻转二叉树
- LintCode 翻转二叉树
- 翻转二叉树-LintCode
- lintcode翻转二叉树
- LintCode - 175.翻转二叉树
- [LintCode]175.翻转二叉树
- 二叉树专题-lintcode翻转二叉树
- 算法题:lintcode#175 翻转二叉树(java)
- LintCode --invert-binary-tree(翻转二叉树)
- LintCode--翻转二叉树(非递归)
- linux下gsoap编译onvif,实现discovery探测设备
- linux--防火墙配置
- 设计模式之Builder模式
- 其他题目---设计RandomPool结构
- spring整合mybatis逆向工程+PageHelper插件
- [Lintcode] #175 翻转二叉树
- 高斯消元集合
- 用this调用构造函数
- ES 基本使用《三》--terms
- 解决svn中“工作副本已经锁定”,或者svn清理失败的解决方法
- Spinner自定义字体
- 《android日常bug系列》android studio意外重启后 原本的import全部报错
- 阿里云linux服务器支持ipv6为苹果过审
- Web过滤器