226. Invert Binary Tree
来源:互联网 发布:淘宝刷钻兼职可信吗 编辑:程序博客网 时间:2024/05/20 18:01
Invert a binary tree.
First of all, consider recursive solution.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode invertTree(TreeNode root) { if(root = null) return null; TreeNode lft = root.left; root.left = invertTree(root.right); root.right = invertTree(root.lft); return root; }}
Also, we can use queue to do BFS.
public TreeNode invertTree(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); if(root = null) return null; queue.offer(root); while(!queue.isEmpty()){ TreeNode now = queue.poll(); TreeNode left = now.left; now.left = now.right; now.right = left; if(node.left != null){ queue.offer(now.left); } if(node.left != null){ queue.offer(now.right); } } return root;}
阅读全文
0 0
- 226.Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 微信支付如何获取微信版本号
- Python多线程setDaemon(True)与join()区别
- 云数据库Redis单节点集群上线--超凡性价比,满足百万QPS,针对超大型纯缓存业务定制
- 完美购物车
- Linux系统根据网段信息查询网卡
- 226. Invert Binary Tree
- “数据中心迁移到IDC数据中心”最怕遇到这几个问题
- 安装MySQL5.6.27数据库
- 读顶级会议上的论文---适应于机器学习、计算机视觉等人工智能领域
- 配电房来了个傻员工,改变了所有聪明的员工!
- Jmeter监控服务器性能
- 测试随感
- Intellij IDEA 快捷键大全
- T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)