LeetCode Invert Binary Tree OJ 刷题 算法
来源:互联网 发布:hid门禁软件 编辑:程序博客网 时间:2024/05/18 03:13
Invert Binary Tree
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \
Solution
import java.util.ArrayList;import java.util.List;public class InvertBinaryTree { public static void main(String[] args) { TreeNode r0 = new TreeNode(8); TreeNode r11 = new TreeNode(4); TreeNode r12 = new TreeNode(10); TreeNode r21 = new TreeNode(2); TreeNode r31 = new TreeNode(1); TreeNode r23 = new TreeNode(9); TreeNode r24 = new TreeNode(12); r0.left = r11; r0.right = r12; r11.left = r21; r12.left = r23; r12.right = r24; r21.left = r31; // outputTree(r0); // invertTree(r0); // outputTree(r0); // TreeNode rr0 = new TreeNode(4); TreeNode rr11 = new TreeNode(2); TreeNode rr12 = new TreeNode(7); TreeNode rr21 = new TreeNode(1); TreeNode rr22 = new TreeNode(3); TreeNode rr23 = new TreeNode(6); TreeNode rr24 = new TreeNode(9); rr0.left = rr11; rr0.right = rr12; rr11.left = rr21; rr11.right = rr22; rr12.left = rr23; rr12.right = rr24; // outputTree(rr0); // invertTree(rr0); // outputTree(rr0); TreeNode q0 = new TreeNode(2); TreeNode q11 = new TreeNode(3); TreeNode q21 = new TreeNode(1); q0.left = q11; q11.left = q21; outputTree(q0); invertTree(q0); outputTree(q0); } public static TreeNode invertTree(TreeNode root) { if (root == null) { return null; } if (root.left != null && root.right != null) { int temp = root.left.val; root.left.val = root.right.val; root.right.val = temp; TreeNode tempLeft = root.left.left; root.left.left = root.right.left; root.right.left = tempLeft; TreeNode tempRight = root.left.right; root.left.right = root.right.right; root.right.right = tempRight; invertTree(root.left); invertTree(root.right); } if (root.left == null && root.right == null) { return root; } if (root.left == null) { root.left = new TreeNode(root.right.val); root.left.left = root.right.left; root.left.right = root.right.right; root.right = null; invertTree(root.left); return root; } if (root.right == null) { root.right = new TreeNode(root.left.val); root.right.left = root.left.left; root.right.right = root.left.right; root.left = null; invertTree(root.right); return root; } return root; } public static void outputTree(TreeNode root) { if (root == null) { return; } System.out.println(root); List<TreeNode> former = new ArrayList<TreeNode>(); former.add(root); while (former.size() != 0) { List<TreeNode> current = new ArrayList<TreeNode>(); for (TreeNode node : former) { System.out.print(node.left + ", "); System.out.print(node.right + ", "); if (node.left != null) { current.add(node.left); } if (node.right != null) { current.add(node.right); } } System.out.println(); former.clear(); former.addAll(current); } }}
反思
这道题 错了几次, 自己做出来了
先是定义了一个输出 tree 的方法, 便于查看是否 invert 成功
几次错误的原因
//wrong answerif (root.left == null) { root.left = new TreeNode(root.right.val); root.left.left = root.right.left; root.left.right = root.right.right; root.right = null; //wrong answer }if (root.right == null) { root.right = new TreeNode(root.left.val); root.right.left = root.left.left; root.right.right = root.left.right; root.left = null; //wrong answer}
这儿是因为上面判断了 left 为 null 的情况, 但是将 right 置为null, 会进到下面的 right == null 的if 块 里
要在
if (root.left == null) {...}
最末加上
return root;
第二次错误 原因
还是上面的代码块,
//wrong answerif (root.left == null) { root.left = new TreeNode(root.right.val); root.left.left = root.right.left; root.left.right = root.right.right; root.right = null; //invertTree(root.left); return root; }if (root.right == null) { root.right = new TreeNode(root.left.val); root.right.left = root.left.left; root.right.right = root.left.right; root.left = null; //invertTree(root.right); return root;}
你会发现加了 return root 后, 如果 left 为空的话, 它和 right 交换后, 方法就返回了
它们的子节点并没有交换
要加上递归语句
invertTree(root.right);
0 0
- LeetCode Invert Binary Tree OJ 刷题 算法
- <LeetCode OJ> 226. Invert Binary Tree
- [leetcode oj 226]Invert Binary Tree
- LeetCode OJ 226. Invert Binary Tree
- [leetcode] Invert Binary Tree
- [LeetCode]Invert Binary Tree
- leetcode:Invert Binary Tree
- LeetCode Invert Binary Tree
- LeetCode | Invert Binary Tree
- leetcode--Invert Binary Tree
- [leetcode] Invert Binary Tree
- [LeetCode] Invert Binary Tree
- LeetCode Invert Binary Tree
- leetcode : Invert Binary Tree
- [LeetCode] Invert Binary Tree
- [Leetcode]Invert Binary Tree
- LeetCode: Invert Binary Tree
- LeetCode Invert Binary Tree
- HDU 5157 Harry and magic string 【Palindromic Tree】
- <五>java数据结构与算法 对象排序
- 学习笔记(九)并发(二)
- 字符串通配符
- 雪花模型,星型模型
- LeetCode Invert Binary Tree OJ 刷题 算法
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
- 【大数据处理架构】3. kafka安装与使用
- 浅谈HTTP中Get与Post的区别
- Python2.7 安装numpy报错解决方法
- Poj--3080Blue Jeans+KMP水题
- mysql如何修改默认字符集编码
- codeforces 510D D. Fox And Jumping(dp+数论)
- Codeforces Round #316 (Div. 2) D Tree Requests