LeetCode-226. Invert Binary Tree
来源:互联网 发布:asp.net管理系统源码 编辑:程序博客网 时间:2024/05/12 12:44
226. Invert Binary Tree
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1Trivia:
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 a whiteboard so fuck off.
就是一个翻转二叉树,递归的程序自己还是写不出来,感觉一片浆糊,o(╥﹏╥)o
非递归:
/** * 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; Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); while (!queue.isEmpty()) { TreeNode current = queue.poll(); TreeNode temp = current.left; current.left = current.right; current.right = temp; if (current.left != null) queue.add(current.left); if (current.right != null) queue.add(current.right); } return root; }}
public TreeNode invertTree(TreeNode root) { if (root == null) { return null; } TreeNode right = invertTree(root.right); TreeNode left = invertTree(root.left); root.left = right; root.right = left; return root;}
阅读全文
0 0
- LeetCode 226. Invert Binary Tree
- [leetcode] 226.Invert Binary Tree
- [leetcode] 226. Invert Binary Tree
- 【LeetCode】226. Invert Binary Tree
- leetCode 226. Invert Binary Tree
- LeetCode #226. Invert Binary Tree
- [LeetCode]226. Invert Binary Tree
- 226. Invert Binary Tree LeetCode
- LeetCode 226. Invert Binary Tree
- LeetCode 226. Invert Binary Tree
- leetcode:226. Invert Binary Tree
- LeetCode 226. Invert Binary Tree
- [LeetCode]226. Invert Binary Tree
- leetcode 226. Invert Binary Tree
- 【LeetCode】226. Invert Binary Tree
- #leetcode#226. Invert Binary Tree
- LeetCode *** 226. Invert Binary Tree
- leetcode: 226. Invert Binary Tree
- 自己说好不算好 看用户如何评OPPO R7s
- vivo双11史上最大降幅600元 千元闪充Y35上市
- 智能锁刷爆淘宝众筹纪录,背后藏着什么商业逻辑?
- 性能测试总结---基础理论
- qbxt day6
- LeetCode-226. Invert Binary Tree
- 大战C100K之4-Linux内核调优篇
- Leetcode [300. Longest Increasing Subsequence]
- Vue-router的使用和出现空白页
- 腾讯安全大数据:双11前黑三天 剁手党最易中招
- 哎呦 不错哦,电源竟然 “装B”成“跑车”
- LeetCode刷题(27)
- 学习MySQL的一些记录笔记(1)10-28
- UVa 213 (implement)